0

My regular expression in Python below doesn't see the pattern. Can you please tell me what I am doing wrong here?

================

import re

l = 'rootfs on / type rootfs (rw xyz'
m_obj = re.match(r'on / type .*? \(rw', l)

if m_obj:
    print "Found!"
else:
    print "Not found!"

=================

Thanks

user1972031
  • 547
  • 1
  • 5
  • 19

3 Answers3

2

have a look at the documentation of the re module - especially the difference between match and search. what you should use here is search (your regex does not match the whole string):

import re

l = 'rootfs on / type rootfs (rw xyz'
m_obj = re.search(r'on / type .*? \(rw', l)

if m_obj:
    print "Found!"
else:
    print "Not found!"
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
1

match, start matching from the beginning of string

you need to use search. It should be:

re.search(r'on / type .*? \(rw', l)
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
1

In fact match starts from the beginning of string- so try

import re

l = 'rootfs on / type rootfs (rw xyz'
m_obj = re.match(r'.*?on / type .*? \(rw', l)

if m_obj:
    print "Found!"
else:
    print "Not found!"
Learner
  • 5,192
  • 1
  • 24
  • 36