python - Use regular expressions to match any number of characters as long as the first character isn't a digit -


i'm trying figure out how use regular expressions match number of characters long first character isn't number. want match if there no values in portion of string i'm checking (the end of it).

if this:

pattern = re.compile (value + r"\d.*") 

it works if string has @ least 1 (non-digit) value after match value.

if this:

pattern = re.compile (value + r"\d?.*") 

it match if first value digit.

i'm trying find match 0 values @ end of string, long first value isn't digit.

examples:

check "abc123" against:

"abc123" : match
"abc123ab" : match
"abc1234" : not match
"abc1234a" : not match
"abc123a4" : match

sounds want "negative lookahead assertion" (search term here).

re.compile(value + '(?!\d)') 

that literally means "not (directly) followed digit".


alternatively, without regular expressions (basic idea taken tigerhawkt3):

not teststring[len(value):][:1].isdigit() 

this assumes test string starts value (if that's not guaranteed, you'd have check it).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -