Home  >  Article  >  Backend Development  >  How to use python regular expressions

How to use python regular expressions

PHP中文网
PHP中文网Original
2017-06-21 16:41:091486browse

Python's regular expression is supported by the re module.

Three matching functions

match: only matches the regular expression from the beginning of the string, and returns matchobject if the match is successful, otherwise it returns none;

re.match(pattern, string, flags=0) ##flags flag bit, used to control the matching method of regular expressions , such as: whether to be case-sensitive, multi-line matching, etc.

search: Try to match all the strings of the string with the regular expression. If all the strings do not match successfully, return none, otherwise return matchobject; (re.search is equivalent to perl Default behavior)

findall method returns a list of all matching expressions;

Use

mypatten = re.compile("Rule") ##Define matching rules

myresult = mypatten.match("String") ##Matching results

if myresult:

 print myresult.group()##You can fill in the numbers in the brackets or name the group (? Pregular expression)#name is a legal identifier

##search is the same as match

mypatten = re.compile("Rule") ##Define matching rules

myresult = mypatten. findall("string") ##Returns a list. If there are groups, it returns a two-dimensional list

##if myresult:

print

myresult.group()

The above is the detailed content of How to use python regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn