A python regular expression, or a memo of a match object

<!-Link-> [1]:https://docs.python.jp/3/library/re.html#match-objects

reference

[6.2. Re — Regular Expression Manipulation — Python 3.6.1 Documentation] [1]

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # The entire match
'Isaac Newton'
>>> m.group(1)       # The first parenthesized subgroup.
'Isaac'
>>> m.group(2)       # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)    # Multiple arguments give us a tuple.
('Isaac', 'Newton')

Symbolic group (named group, named capture)

[6.2. Re — Regular Expression Manipulation — Python 3.6.1 Documentation] [1]

|Context to reference group “quote”|Reference method| |:---|:---| |Reference to the same pattern|・(?P=quote)(As it is)
\1 | |Match objectmAt the time of processing|・m.group('quote')
m.end('quote')(etc.)| |re.sub()String passed to the repl argument of|・\g<quote>
\g<1>
\1|

Behavior of match.group (), match.groupdict ()

Sample to examine the behavior

example.txt


 .text            000c4342+000000f0 _ccc

Regular expression ("(? P <addr> [0-9A-Fa-f] {8}) \ + (? P <size> [0-9A-Fa-] to match 000c4342 + 000000f0 If you use f] {8}) "),

In [217]: with open("example.txt") as f:
     ...:     for s in f:
     ...:         m = re.search("(?P<addr>[0-9A-Fa-f]{8})\+(?P<size>[0-9A-Fa-f]{8})",s)
     ...:         if m:
     ...:             print("m.groups()        : " + str(m.groups()))
     ...:             print("m.group()         : " + str(m.group()))
     ...:             print("m.group(0)        : " + str(m.group(0)))
     ...:             print("m.group(1)        : " + str(m.group(1)))
     ...:             print("m.group(2)        : " + str(m.group(2)))
     ...:             print("m.groupdict()     : " + str(m.groupdict()))
     ...:             print("m.group(\"addr\")   : " + str(m.group("addr")))
     ...:             print("m.group(\"size\")   : " + str(m.group("size")))
     ...:
     ...:
m.groups()        : ('000c4342', '000000f0')
m.group()         : 000c4342+000000f0
m.group(0)        : 000c4342+000000f0
m.group(1)        : 000c4342
m.group(2)        : 000000f0
m.groupdict()     : {'addr': '000c4342', 'size': '000000f0'}
m.group("addr")   : 000c4342
m.group("size")   : 000000f0

Recommended Posts

A python regular expression, or a memo of a match object
python regular expression memo
I tried to make a regular expression of "amount" using Python
Start / end match in python regular expression
Connect a lot of Python or and and
I tried to make a regular expression of "date" using Python
Effective Python Memo Item 4 Write a helper function instead of a complicated expression
A memo of a tutorial on running python on heroku
A python lambda expression ...
Regular expression in Python
Make one repeating string with a Python regular expression.
A memo connected to HiveServer2 of EMR with python
Python: Get a list of methods for an object
A regular expression that finds N or more consecutive substrings of the same character
[python] Value of function object (?)
Python 處 處 regular expression Notes
Regular expression manipulation with Python
Determine if a string is a time with a python regular expression
How to develop in a virtual environment of Python [Memo]
A record of patching a python package
[Python] A memo of frequently used phrases (by myself) in Python scripts
A good description of Python decorators
[Python] Operation memo of pandas DataFrame
Touch a Python object from Elixir
A memo that handles double-byte double quotes in Python regular expressions
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
A python regular expression, str and unicode that are sober and addictive
String replacement with Python regular expression
[Python] Creating a scraping tool Memo
Separate display of Python graphs (memo)
[Python] return A [or / and] B
[Python memo] Be careful when creating a two-dimensional array (list of lists)
URL match checking and extraction with python regular expression Regex Full version
Features of regular expression modules that I often use personally in Python
How to format a list of dictionaries (or instances) well in Python
Use print in a Python2 lambda expression
A rough understanding of python-fire and a memo
Display a list of alphabets in Python 3
Make a relation diagram of Python module
Create a JSON object mapper in Python
[Python3] Rewrite the code object of the function
AtCoderBeginnerContest154 Participation memo (Python, A ~ E problem)
(Python) HTML reading and regular expression notes
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Non-logical operator usage of or in python
A memo explaining the axis specification of axis
The story of blackjack A processing (python)
Memo of troubles about coexistence of Python 2/3 system
[Python] Get a list of folders only
A memorandum of python string deletion process
A story made by a person who has no knowledge of Python or Json
Python memo
python memo
Python memo
Python memo
Python memo
Python memo
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Machine learning memo of a fledgling engineer Part 1