2025高考文綜真題
題目:給定一個字符串,用正則表達式提取其中的文本。
要求:
1. 使用正則表達式提取文本。
2. 提取的文本必須包含在給定的字符串中。
3. 每行字符串不超過1000個字符。
4. 可以使用Python編寫程序實現。
示例:
給定字符串:
“`
hello, world!
“`
用正則表達式提取其中的文本:
“`
\\w+
“`
提取的文本:
“`
hello, world!
“`
示例程序:
“`python
import re
text = \”hello, world!\”
pattern = r\”\\w+\”
result = re.search(pattern, text)
if result:
print(result.group(0))
else:
print(\”Text not found.\”)
“`
輸出:
“`
hello, world!
“`
注意:
– `\\w` 匹配任何字母,數字,下劃線,空格和其他字符。
– `+` 匹配前面的字符一次或多次。
– `( )` 匹配前面的字符一個或多個。