How to catch parts of a string in Regex in Python 3
Question:
I’m trying to build a regex in Python that will takes parts of a string.
For example:
(% aaa bbb cc %) dsds (% dd %)
I want to get at the end two sub-strings:
- aaa bbb cc
- dd
i.e. – the strings between the (%
and %)
.
I try this one: ((%.*%))
– but it takes all the line :-
import re
a = "(% aa bbb %) dsds (% ccc %)"
x = re.search("((%.*%))", a)
print(x.group())
Can you help me with this please?
Thank you!
Answers:
-
You need to use the ?
modifier to get the smallest possible match instead of the largest (known as ‘lazy’ or ‘non-greedy’)
-
Use .findall
because you will get multiple matches
a = "(% aa bbb %) dsds (% ccc %)"
x = re.findall("(%s(.*?)s%)", a)
print(x)
outputs
['aa bbb', 'ccc']
See more explanations and live demo on regex101.com.
I’m trying to build a regex in Python that will takes parts of a string.
For example:
(% aaa bbb cc %) dsds (% dd %)
I want to get at the end two sub-strings:
- aaa bbb cc
- dd
i.e. – the strings between the (%
and %)
.
I try this one: ((%.*%))
– but it takes all the line :-
import re
a = "(% aa bbb %) dsds (% ccc %)"
x = re.search("((%.*%))", a)
print(x.group())
Can you help me with this please?
Thank you!
-
You need to use the
?
modifier to get the smallest possible match instead of the largest (known as ‘lazy’ or ‘non-greedy’) -
Use
.findall
because you will get multiple matches
a = "(% aa bbb %) dsds (% ccc %)"
x = re.findall("(%s(.*?)s%)", a)
print(x)
outputs
['aa bbb', 'ccc']
See more explanations and live demo on regex101.com.