python - Regex for uppercase and underscores between percentage signs -


regex has never been strong point. in python i'm attempting build expression matches substrings such this:

%match%

%match_1%

$this_is_a_match%

it extracted %match% or %like_this%

i ended (logically, not seem work): %[a-z0-9_]*$%

so going wrong on this?

you can use simple regex this:

[%$]\w+[%$]   <-- notice put $ because of sample 

on other hand, if want uppercase can use:

[%$][a-z_\d]+[%$] 

if want match content within %, use:

%.+?% 

python code

import re p = re.compile(ur'[%$]\w+[%$]') test_str = u"%match%\n\n%match_1%\n\n$this_is_a_match%"  re.findall(p, test_str) 

btw, problem regex below:

%[a-z0-9_]*$%            ^--- remove dolar sign 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -