Home > database >  Regex: Excluding tokens that contain only digits
Regex: Excluding tokens that contain only digits

Time:10-31

I want to get all tokens that aren't only digits - so 'abc' and 'abc7' and '123xyz' are ok but '1234' isn't.

I know you can use

\b\d \b

to get just '1234' but when I try adding '^' to the front of that it doesn't match anything.

CodePudding user response:

You can use:

(?!^\d $)^. $

This is a negative lookahead, it will exclude all strings that start with digits and end with digits.

If your string start with a letter, it will be excluded as not matching ^\d , which means it will be accepted. If your string ends with a letter, it will be excluded as not matching \d $, which means it will be accepted. Then the remaining part of the expression (^. $) will match absolutely anything. If the string is only digits, it will match with the negative-lookahead and not the last part, which means it will return a non-match result.

So basically, only digits strings will not match.

CodePudding user response:

Try (regex101):

(?!\b\d \b)\b\S \b

This matches all tokens that aren't only digits.

  • Related