Python Regular Expression objects have many problems in our use and need to be noticed. Next, let's take a look at how to apply Python Regular Expression objects to related technologies. Hope to help you.
Generation Method: Pass
- Re. compile (pattern, [flags]) Back
- Match (string [, pos [, endpos]); returns string [pos, endpos] matching
- MatchObject of pattern
Python code
- split( string[, maxsplit = 0])
- findall( string[, pos[, endpos]])
- sub( repl, string[, count = 0])
These functions are the same as those in the re module, but the calling form is slightly different.
Re. Several Functions and several functions of the regular expression object have the same function, but if the same program
Using these functions multiple times, several functions of the regular expression object are more efficient.
Matchobject
Use re. match (......) And re. compile (......). Match return
This object has the following methods and attributes:
Method:
- group( [group1, ...])
- groups( [default])
- groupdict( [default])
- start( [group])
- end( [group])
The best way to illustrate these functions is to give an example.
- matchObj = re.compile(r”(?P\d+)\.(\d*)”)
- m = matchObj.match(’3.14sss’)
- #m = re.match(r”(?P\d+)\.(\d*)”, ‘3.14sss’)
- print m.group()
- print m.group(0)
- print m.group(1)
- print m.group(2)
- print m.group(1,2)
- print m.group(0,1,2)
- print m.groups()
- print m.groupdict()
- print m.start(2)
- print m.string
The output is as follows:
- 3.14
- 3.14
- 3
- 14
- (’3′, ‘14′)
- (’3.14′, ‘3′, ‘14′)
- (’3′, ‘14′)
- {’int’: ‘3′}
- 2
- 3.14sss
Therefore, group () and group (0) are returned, which match the string of the entire expression. In addition, group (I) is the regular expression that uses the '3. 14', '3', '14') can best illustrate the problem.