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.
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',)
import re
with open(infile) as f:
print re.findall('^(0D.{15}|\d{15})$',f.read(),re.MULTILINE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With