regex - python regular expression $[a-zA-Z ][0-9a-zA-Z ]* with forbidding -
i looking regular expression in python, in string finds me:
$[a-za-z_][0-9a-za-z_]*
there can more of , can seperated whitespaces (\s)
this quite easy, need forbid whole string if there did not match pattern. (+ empty string ok)
i'll give examples:
$x$y0123 => ok, gives me [$x, $y0123] $ => bad (only $) "" or " \t" => ok, gives me [] $x @hi => bad, cause @hi, not match pattern
it can more regular expressions, doesn't have one.
regex = re.compile("(\$[a-za-z_][0-9a-za-z_]*)") regex.findall(string)
this ok, if don't have check things also.
hmm, i'm not entirely sure you're trying do, maybe need 2 regex: first check validity of format, , second retrieve matches.
import re stuff = ["$x$y0123", "$", "", " \t", "$x @hi"] p1 = re.compile(r'(?:\$[a-z_]\w*|\s)*$', re.ignorecase) p2 = re.compile(r'\$[a-z_]\w*|\s+', re.ignorecase) thing in stuff: if p1.match(thing): print(p2.findall(thing))
will print:
['$x', '$y0123'] [] [' \t']
Comments
Post a Comment