MST

星途 面试题库

面试题:Python正则匹配特定起始和结尾字符串

给定字符串 'hello world, this is a test string', 请使用Python的正则表达式匹配以 'h' 开头且以 'g' 结尾的子字符串,写出Python代码实现。
29.5万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
import re

string = 'hello world, this is a test string'
matches = re.findall(r'\bh\S*g\b', string)
print(matches)