Many times we need to match whether the user input meets certain requirements (such as email matching, phone number matching), or we need to filter out files with certain requirements according to the requirements (such as finding out the picture files of a certain day from a variety of files) At this time, we need to manually define the specification, which can match the string that meets the requirements, which is the regular expression we are going to talk about today.
This article uses python's re package for regular matching
import re
- Direct match
re.match('hello','hello').group() # match 前一个引号内为规则,后一个引号内为待匹配对象
The output is:
'hello'
But this also has drawbacks
re.match('hello','hello,world').group()
The output is still:
'hello'
That is, it only matches to the end of the previous string, and does not care about it later.
Of course, the cases that cannot be matched are as follows:
re.match("hello","holle,world").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-7f1d9f73b36d> in <module>
----> 1 re.match("hello","holle,world").group()
AttributeError: 'NoneType' object has no attribute 'group'
- Single-character matching rules
character | Features |
---|---|
. | Match any character |
[ ] | Match the characters listed in [] |
\d | Match numbers, ie 0-9 |
\D | Match non-digit |
\s | Match space, tab key |
\S | Match non-space, tab key |
\w | Match word characters, ie 0-9, az, AZ, _ |
\W | Match non-word characters |
- Match multiple characters
character | Features |
---|---|
* | Matches the previous character zero or unlimited times, which is optional |
+ | Match the previous character once or unlimited times, that is, at least once |
? | Match the previous character one or zero times, that is, at most once |
{m} | Match the previous character m times |
{m,n} | Matches the previous character mn times |
- Match beginning and end
symbol | Features |
---|---|
^ | Match beginning |
$ | End of match |
- Match grouping
symbol | Features |
---|---|
| | Match either left or right expression |
( ) | Use the characters in the brackets as a group |