Reputation: 79
I'm trying to pull a specific recurring item from emails using regex and python, the pattern is always:
OS - TYPE - VER - en - he_IL - 1.1.2 - U: username - hash
i was trying to do it with the following condition:
if re.search('U: \s*( - )', message_body)
hoping to get "username" out, unfortunately it didn't give out anything.
also, trying if re.search('U: \w*())', message_body)
gave me response that was too broad and included the actual "U: " with the username.
i would love to get some pointers that does not include link to the manual.
Upvotes: 2
Views: 56
Reputation: 180540
You can use split
:
s = "OS - TYPE - VER - en - he_IL - 1.1.2 - U: username - hash"
print (s.split("U: ")[1].split()[0])
username
Or using re:
import re
(re.findall(" U:\s+(\w+)",s)[0])
username
re is considerably slower:
In [20]: timeit (re.findall(" U:\s+(\w+)",s)[0])
100000 loops, best of 3: 2.5 µs per loop
In [21]: timeit (s.split("U: ")[1].split()[0])
1000000 loops, best of 3: 764 ns per loop
Upvotes: 1
Reputation: 67988
U:\s*(\S+)
Try this.Use print re.search(r"U:\s*(\S+)",x).group(1)
to get username
.
Here x
is your string.
See demo.
http://regex101.com/r/lS5tT3/73
Upvotes: 1
Reputation: 19641
Use a capture group with an actual expression:
match = re.search('U:\s*(\S+)')
if match: username = match.group(1)
match = re.search('U:\s*(\S+ - \S+)')
if match: username_and_hash = match.group(1)
match = re.search('U:\s*(\S+) - (\S+)')
if match:
username = match.group(1)
userhash = match.group(2)
Upvotes: 1