First, the regular expression: common method

1, re.match (r "hello", "hello, world") # indicates that they r "" content to match the contents of the back, if matched, and will return an object, otherwise, there is no return value. .group () can be matched to the content extracted.

2、re.search()

. 3, the re.findall ()
the re.findall ( ".") Matches any character, but does not match "/ n", can be matched together re.S: "." Re.findall (, " / n" , re.S), the output / n-
the re.findall ( "a (. ?) D", "ABCD"), the output of BC,. " ?" matches any of the content, and can play characters before or after positioning the brackets, and screening effect

4, re.sub (), the content may be replaced
Here Insert Picture Description
digital b is replaced with an underscore or directly remove

5, re.comlile () can compile the content, when the program is executed many times can save operating time
Here Insert Picture Description
6, r is the meaning of the original string, after adding, direct representation of the original character, \ n newline no longer represents only Mean surface. Multi-path for the regular expression or wins

7, "\ s" will match any whitespace, including "\ n" "\ t" "\ r"

8, non-greedy matching only need to add in the back? To

Published 47 original articles · won praise 74 · views 7907

Guess you like

Origin blog.csdn.net/Jacky_kplin/article/details/104744979