Regular expression for (= string)

I have a text file containing thousands of lines. here is an example

line = .Falies/367. 11DG1550/11DG15537.Axiom=nt60
line = .Failies/367. 11DG1550/11DG15537.Axiom=nt50

I tried to extract the line at the end of 'nt60', 'nt50'.

lines = line.split('=')
version = lines[-1]

the problem is that the line break character will be included ( '\n')

I was thinking about using regex search to match a string starting with ( '=nt') but I have no idea what to use for matching =, word, number.

Can anyone help?

+4
source share
2 answers

The regular expression matches = nt, then a number:

=(nt\d+)

And in your example:

line = .Falies/367. 11DG1550/11DG15537.Axiom=nt60 
line = .Failies/367. 11DG1550/11DG15537.Axiom=nt50 

it will return two matches:

MATCH 1
1.  [49-53] `nt60`
MATCH 2
1.  [105-109] `nt50`

Explanation:

`=` matches the character `=` literally 
1st Capturing group `(nt\d+)`
   `nt` matches the characters `nt` literally (case sensitive)  
   `\d` match a digit `[0-9]`  
   `+` Quantifier: Between one and unlimited times, as many times as possible,  
       giving back as needed  

, a = word number, nt \w+, .

, .

+1

. , , , strip():

strip() .

>>> your_str = 'nt60\n'
>>> your_str.strip()
'nt60'

:

lines = line.rsplit('=',1)
version = lines[-1].strip()
+2

Source: https://habr.com/ru/post/1534107/


All Articles