Emil Brundage
Emil Brundage

Reputation: 213

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 to use regular expressions to match any number of characters as long as the first character isn't a number. I also want it to match if there are no values in the portion of the string I'm checking (the end of it).

If I do this:

Pattern = re.compile (value + r"\D.*")

It only works if the string has at least one (non-digit) value after the match value.

If I do this:

Pattern = re.compile (value + r"\D?.*")

It will match even if the first value is a digit.

I'm trying to find a match for zero to any values at the end of the string, as long as the first value isn't a digit.

Examples:

Check "abc123" against:

"abc123" : Match
"abc123ab" : Match
"abc1234" : Not a match
"abc1234a" : Not a match
"abc123a4" : Match

Upvotes: 2

Views: 3045

Answers (4)

karthik manchala
karthik manchala

Reputation: 13640

You can use the following .. just added a ? (zero or one match) for your entire pattern than just \D:

Pattern = re.compile (value + r"(\D.*)?$")

Upvotes: 0

Stefan Pochmann
Stefan Pochmann

Reputation: 28596

Sounds like you want a "negative lookahead assertion" (search that term here).

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

That literally means "not (directly) followed by a digit".


Alternatively, without regular expressions (basic idea taken from TigerhawkT3):

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

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

Upvotes: 2

TigerhawkT3
TigerhawkT3

Reputation: 49320

Without regex:

def check(string, value):
    return not string[len(value)].isdigit() if len(value)<len(string) else True

Results:

check('abc123', 'abc123')
True
check('abc123ab', 'abc123')
True
check('abc1234', 'abc123')
False
check('abc1234a', 'abc123')
False
check('abc123a4', 'abc123')
True

Upvotes: 1

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You can simply use an alternative between \D and the end of the string:

re.compile (value + r"(?:\D|$)")

Or this if you need to match until the end of the string:

re.compile (value + r"(?:\D.*|$)")

This will match the first digit or the end of the string.

It would also work with a lookahead if you don't need the whole string to be matched, only if you need to check if it's a match or not:

re.compile (value + r"(?=\D|$)")

Upvotes: 0

Related Questions