MST

星途 面试题库

面试题:Python正则匹配多个字符串基础应用

假设有一个字符串 'The quick brown fox jumps over the lazy dog. Python is great, and Java is also good.', 请使用Python的正则表达式匹配出其中的 'Python' 和 'Java' 这两个字符串,并将匹配结果以列表形式输出。
21.5万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
import re

string = 'The quick brown fox jumps over the lazy dog. Python is great, and Java is also good.'
matches = re.findall(r'(Python|Java)', string)
print(matches)

上述代码使用re.findall函数和正则表达式(Python|Java)在给定字符串中查找所有匹配的PythonJava,并将结果以列表形式输出。

输出结果类似:

['Python', 'Java']