Python regular expression
A regular articulation is an extraordinary succession of characters that encourages you effectively check if a string matches an example.
Python has included the re module since form 1.5, which gives a Perl-style regular articulation design.The
re module gives the Python language loaded with standard articulation usefulness.The
compile work produces a regular articulation object dependent on an example string and discretionary banner parameters. This article has a lot of techniques for customary articulation coordinating and substitution.The
re module additionally furnishes capacities that are completely useful with these strategies, utilizing an example string as their first contention.
This part centers around regular articulation preparing capacities usually utilized in Python.
re.match attempts to match a pattern from the beginning of the string. If the start position is not matched successfully, match() returns none.
re.match Span>(pattern, < /span>string, flags =0)
Function parameter description:
|pattern||matching customary expressions|
|string||The string to coordinate.|
|flags||banner is utilized to control how standard articulations are coordinated, for example, regardless of whether to recognize capitalized and lowercase, multi-line coordinating, etc. See: Regular Expression Modifiers - Optional Flags|
The coordinating achievement re.match strategy restores a coordinating article, else it returns None.
We can utilize the group(num) or gatherings() coordinating article capacities to get the coordinating articulation.
|Matching object methods||Description|
|group(num=0)||matches the string of the whole articulation, gathering() can enter different gathering numbers at once, in which case it will return one containing those The tuple of the esteem relating to the gathering.|
|groups()||Returns a tuple containing all the gathering strings, from 1 to the included group number.|
The above example runs the output as:
(0, 3) None
The above example execution results are as follows：
matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter