Tips for regular expression (Python )

'^' (Caret.) Matches the start of the string, and inMULTILINE mode also matches immediately after each newline.

>>> re.findall(r'^th[\w ]+', """

... This line is the first,

... another line,

... that line, it's the best

... """)

[]

 

行首没有r'^th[\w ]+'

>>> re.findall(r'(?m)^th[\w ]+', """

... This line is the first,

... another line,

... that line, it's the best

... """)

['that line']

多行行首匹配

>>> re.findall(r'(?i)th.+', ''' The first line the second line the third line ''') ['The first line', 'the second line', 'the third line']

搜索全部

re.search(r'th.+', '''

The first line

the second line

the third line

''').group()

'the second line '

只搜索第一个

re.match(r'(?im)th.+', ''' The first line the second line the third line ''').group()

行首没有r'(?im)th.+'

评论

© ID4333709 | Powered by LOFTER