[Tutor] regex advice

Cameron Simpson cs at zip.com.au
Tue Jan 6 22:22:35 CET 2015


On 06Jan2015 11:43, Norman Khine <norman at khine.net> wrote:
>i have the following code:
[...]
>#t(" ")
>gettext_re = re.compile(r"""[t]\((.*)\)""").findall

My first thought is: "[t]"  can just be written "t"
 
[...]
>so, gettext_re = re.compile(r"""[t]\((.*)\)""").findall is not correct as
>it includes
>results such as input( type="hidden" name="_csrf" value=csrf_token )
>
>what is the correct way to pull all values that are within t(" ") but
>exclude any tt( ) and input( )

Others have suggested word boundaries and nongreedy matches. You don't need 
them.

Use this:

  \([a-zA-Z]\w*\)(\([^)]*\))

which matches any "foo(blah)".

Then look at the returned match object and check that .group(1) == "t".

Cheers,
Cameron Simpson <cs at zip.com.au>

I heard a funny one this weekend.  I was belaying a friend on a very short
problem and when she was pumped out she told me to "Let me down" and my
other friend that was standing nearby said.  "You were never UP!".
        - Bryan Laws <bryanlaws at aol.com>


More information about the Tutor mailing list