trickabug
trickabug

Reputation: 3

Regular expression to identify first character only

I'm trying to identify medical diagnosis codes that start with either 8 or 9, without regard to what comes afterwards. They might be formatted as 800.1, 956.35, etc.

Our programmer got me started with [89][0-9][0-9]* but that appears to be identifying anything with an 8 or 9 in it.

Dividing the searches up would be fine. I tried using a simple ^[8], ^8, ^[9], ^9, and all of those found 800 in the tester (I'm using regexpal.com)...but nothing else (it didn't find 850, etc).

Thanks!

Upvotes: 0

Views: 169

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

Perhaps, you already a regex guru, I will answer just pro forma.

Just use a ^[89] regular expression to only match strings that start with 8 or 9.

Here, ^ is an anchor that tells the regex engine to match at the beginning of a string (or line if multiline option is enabled, or (?m) is added at the beginning of the regex pattern).

[89] is a non-negated character class that will match every character inside it (or a range, if the range is provided).

A demo has been provided by msrd0.

Upvotes: 1

Related Questions