class GameRole:
def __init__(self, nm, wp):
self.name = nm
self.weapon = wp
if __name__ == '__main__':
lb = GameRole(' Lyu3 bu4 ', ' Fang Tian draws halberds ') # Create a lb Example
print(lb.name, lb.weapon)
Class has some special methods that start and end with double underscores , Also known as magic Magic methods .
https://jex.im/regulex
# take mac Add a colon to the address
192.168.1.1 000C29123456
192.168.1.2 525400A31B2C
192.168.1.3 0002231A08D3
# Ideas : find mac Address 、 Two numbers in groups 、 Colon between groups
:%s/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)$/\1:\2:\3:\4:\5:\6/
re Module common methods
>>> import re
# match Match to , Return match object , Otherwise return to None
>>> re.match('f..', 'food')
<_sre.SRE_Match object; span=(0, 3), match='foo'>
>>> print(re.match('f..', 'seafood'))
None
# search Match in a string
>>> re.search('f..', 'food')
<_sre.SRE_Match object; span=(0, 3), match='foo'>
>>> re.search('f..', 'seafood')
<_sre.SRE_Match object; span=(3, 6), match='foo'>
>>> m = re.search('f..', 'seafood')
>>> m.group() # Match the object group Method returns the matching string
'foo'
# findall You can match everything
>>> re.findall('f..', 'seafood is food')
['foo', 'foo']
# finditer Returns the iterator of the matching object
>>> list(re.finditer('f..', 'seafood is food'))
[<_sre.SRE_Match object; span=(3, 6), match='foo'>, <_sre.SRE_Match object; span=(11, 14), match='foo'>]
>>> for m in re.finditer('f..', 'seafood is food'):
... m.group()
...
'foo'
'foo'
# split For cutting
# With . or - As a separator
>>> re.split('\.|-', 'how-are-you.tar.gz')
['how', 'are', 'you', 'tar', 'gz']
# Replace
# take X Replace with python
>>> re.sub('X', 'python', 'X is good. I like X.')
'python is good. I like python.'
# When there are a lot of matches , Compile the schema ahead of time , You can get better efficiency
>>> patt = re.compile('f..')
>>> patt.search('seafood')
<_sre.SRE_Match object; span=(3, 6), match='foo'>
>>> m = patt.search('seafood')
>>> m.group()
'foo'
>>> patt.findall('seafood is food')
['foo', 'foo']