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:

  1. aaa bbb cc
  2. 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!

Asked By: Yoar

||

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.

Answered By: DeepSpace
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.