Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python re match string in a file

Tags:

python

I want to match all the lines in a file either starting with 0D and has 15 Characters or just having 15 digits. How can i do this

p_number = re.compile(r'(\d{15})')
f=open(infile)
for l in f:
  aa=re.findall(p_number,l)
  if aa > 0:
     print aa
f.close() 

EDIT

If only the pattern is in the starting of the line.

like image 724
Rajeev Avatar asked Dec 07 '22 13:12

Rajeev


2 Answers

To find matches only at the beginning of the line, use re.match. This regex matches all non-whitespace characters if the 0D prefix is present; if you want to match fewer characters, let me know.

>>> p_number = re.compile(r'(0D[\S]{13}|\d{15})')
>>> for s in ['0Dfannawhoopowe foo', 
              'foo 012345678901234', 
              '012345678901234 foo']:
...     match = p_number.match(s)
...     if match:
...         print match.groups()
... 
('0Dfannawhoopowe',)
('012345678901234',)

For a sense of the difference between match, search, and findall, see the following examples.

findall (naturally) finds all occurrences of the match:

>>> for s in ['0Dfannawhoopowe foo', 
              'foo 012345678901234', 
              '012345678901234 foo']:
...     match = p_number.findall(s)
...     if match:
...         print match
... 
['0Dfannawhoopowe']
['012345678901234']
['012345678901234']

search finds an occurrence of the string anywhere in the string, not just at the beginning.

>>> for s in ['0Dfannawhoopowe foo', 
              'foo 012345678901234', 
              '012345678901234 foo']:
...     match = p_number.search(s)
...     if match:
...         print match.groups()
... 
('0Dfannawhoopowe',)
('012345678901234',)
('012345678901234',)
like image 126
senderle Avatar answered Dec 30 '22 21:12

senderle


import re
with open(infile) as f:
 print re.findall('^(0D.{15}|\d{15})$',f.read(),re.MULTILINE)
like image 40
Marco de Wit Avatar answered Dec 30 '22 21:12

Marco de Wit