Hakam Omran
Hakam Omran

Reputation: 21

Python re.findall getting value

I have a text file which have multi lines in the same following pattern

Server:x.x.x # U:100 # P:100 # Pre:00 # Tel:xxxxxx

I built this code to get the value after Pre:

x2 = (re.findall(r'Pre:(\d+)',s))

I'm not so familiar with re patterns , but this code don't get the value if it is + or empty value ( a None value )

Any suggestions to generlize the code to get what ever value after Pre: until the next # without the space ?

Upvotes: 1

Views: 1102

Answers (4)

Pedro Lobito
Pedro Lobito

Reputation: 98881

x2 = (re.findall(r'Pre:(.*?) #',s))

Pre:(.*?) #

Match the character string “Pre:” literally «Pre:» Match the regex below and capture its match into backreference number 1 «(.?)»
Match any single character that is NOT a line break character «.
?» Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “ #” literally « #»

Upvotes: 0

alecxe
alecxe

Reputation: 473813

A non-regex approach would involve splitting by # and then by : forming a dictionary which would make accessing the parts of the string easy and readable:

>>> s = "Server:x.x.x # U:100 # P:100 # Pre:00 # Tel:xxxxxx"
>>> d = dict([key.split(":") for key in s.split(" # ")])
>>> d["Pre"]
'00'

Upvotes: 0

Vyktor
Vyktor

Reputation: 20997

The example you've provided works just fine:

>>> import re
>>> s = 'Server:x.x.x # U:100 # P:100 # Pre:00 # Tel:xxxxxx'
>>> re.findall(r'Pre:(\d+)', s)
['00']

You may need to add handling of +/- and ., for negative numbers and decimals: (-?[\d.,]+).

If you need to match any string (not just numbers) you may want to use Pre:(.*?)\s*#.

Or you may avoid using regexps at all and split row by # separator:

>>> s.split('#')
['Server:x.x.x ', ' U:100 ', ' P:100 ', ' Pre:00 ', ' Tel:xxxxxx']

And then split rows by first ::

>>> for row in s.split('#'):
...     k, v = row.split(':', 1)
...     print(k.strip(), '=', v.strip())
... 
Server = x.x.x
U = 100
P = 100
Pre = 00
Tel = xxxxxx

Upvotes: 0

Jayanth Koushik
Jayanth Koushik

Reputation: 9874

How about this as the pattern? It will get everything until the next " #" but without being greedy (that's what the ? is for).

r"Pre:(.*?) #"

Upvotes: 1

Related Questions