Extracting a number that starts with specific digits from a string variable

Hello,

I ve got a problem. I need to extract a value from a string variable that starts with “19”, “47” or “68”. The number has 10 digits. Can anyone help me with the regex?

I’ve tried this for 19 but it doesnt work. ^19\d{8}$

Thanks a lot

^((19)|(47)|(68))\d{8}

1 Like

The answer from @Anthony_Humphries will work assuming that the whole string starts with 19 or 47, or 68. If that 10 digit number is in the middle of the string it won’t work.

Unless you think there may be 10 digit numbers starting with 19, 47, or 68 within the string you’re searching that you DON’T want to grab, then I would omit the ^ altogether (and also not use the $ unless you ONLY want to grab the 10 digit number at the end of a string). That regex would then just be: (19|47|68)\d{8}

EDIT: The above regex would still grab 11+ digit numbers as well. If that is something you need to avoid, then you should also add a negative lookahead & negative lookbehind. That would be: (?<!\d)(19|47|68)\d{8}(?!\d)

Also, you may need to use the multiline option if you want to use the ^ at the beginning of each line within a single string.

1 Like

@Alperen_Tac Hello,

The following pattern will do the job and ensure to retain number with 10 digits only.

"\b(19|47|68)\d{8}\b"

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.