M
M
Maxim Nikitenko2014-02-05 10:11:48
Python
Maxim Nikitenko, 2014-02-05 10:11:48

How to write regexp correctly?

In general, I need to find all the localization strings in the template, where it happens, I use different quotes, I wrote a script, but here's the problem

import re
text = """<input value="0" >{{ _('Root') }}
<input value="1" >{{ _("Test's") }}
<input value="{{ _('Move') }}" name="submit">"""
regx = re.compile(r"""_\((["'])([^\1]*)\1\)""")
regx.findall(text)

For some reason this regexp doesn't work.
If I do this:
regx = re.compile(r"""_\((["'])([^'"]*)\1\)""")
regx.findall(text)

Naturally, it does not find the second line, bumping into a single quote.
Actually the question is:
How to write regexp correctly and why mine does not work?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Nikitenko, 2014-02-05
@sets88

In general, the solution is:
Thanks to Vintorez for the help!

V
Valery, 2014-02-05
@Vintorez

Like this:

regx = re.compile(r"""_\(['"](?P<value>.+)['"]\)""")

V
Valery, 2014-02-05
@Vintorez

It seems to me that checking for double and single quotes inside is superfluous. I think it will be enough for you to write like this:regx = re.compile(r"""_\(.*\)""")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question