Reputation: 363
fix_release='Ubuntu 16.04 LTS'
p = re.compile(r'(Ubuntu)\b(\d+[.]\d+)\b')
fix_release = p.search(fix_release)
logger.info(fix_release) #fix_release is None
I want to extract the string 'Ubuntu 16.04'
But, result is None.... How can I extract the correct sentence?
Upvotes: 0
Views: 39
Reputation: 10360
Try this Regex:
Ubuntu\s*\d+(?:\.\d+)?
Explanation:
Ubuntu
- matches Ubuntu
literally\s*
- matches 0+ occurrences of a white-space, as many as possible\d+
- matches 1+ digits, as many as possible(?:\.\d+)?
- matches a .
followed by 1+ digits, as many as possible. A ?
at the end makes this part optional.Note: In your regex, you are using \b
for the spaces. \b
returns 0 length matches between a word-character and a non-word character. You can use \s
instead
Upvotes: 1
Reputation: 215127
You confused the word boundary \b
with white space, the former matches the boundary between a word character and a non word character and consumes zero character, you can simply use r'Ubuntu \d+\.\d+'
for your case:
fix_release='Ubuntu 16.04 LTS'
p = re.compile(r'Ubuntu \d+\.\d+')
p.search(fix_release).group(0)
# 'Ubuntu 16.04'
Upvotes: 1