Mohd Ali
Mohd Ali

Reputation: 311

Is there any better way to solve this regular expression in Python?

I've Python list like below,

l = ['ac no **82....383 is as on 6767', 'ac **82....383 in for 23-23']

I've written Python regular expression to extract any number at position of '383' in above elements of list:

for i in l:
    match = re.search(r'ac.*\.([\d]*)\s(is|in)', i)
    if match: print match.group(1)

It works fine. But it won't work for this.

l = ['ac no **82....383 of as on 2737183']

I can solve this by tweaking the same regular expression. But would really appreciate if a better way is available.

Upvotes: 0

Views: 80

Answers (3)

konart
konart

Reputation: 1834

Or you can do it without regex at all, for example:

a = 'ac no **82....383 of as on 2737183'

print a.split(".")[-1].split(" ")[0]

==> 383

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107287

It's because of that you have the word of after your number while you didn't define such situation in your regex (you just defined is or in). you can also add of in group (is|in) or as a general way you can use another regex.

For example you can use r'\.{3,}(\d*) as your regex that match numbers that precede by 3 or more dot.

>>> l = ['ac no **82....383 is as on 6767', 'ac **82....38300 in for 23-23','ac no **82....323 of as on 2737183']
>>> for i in l:
...     match = re.search(r'ac.*\.{3,}(\d*)', i)
...     if match: print match.group(1)
... 
383
38300
323

Upvotes: 0

karthik manchala
karthik manchala

Reputation: 13640

You can use the following:

ac.*?\.([\d]*)\b
    ^         ^
  • ? to make it non greedy
  • \b to make it independent of is, of, in etc..

See DEMO

i.e:

match = re.search(r'ac.*?\.([\d]*)\b', i)

Upvotes: 1

Related Questions