Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to Match All Numbers Except Those in the First Word

I am having trouble crafting a regex. For example, in the string A123 4HEL5P6 789 I want to match all the numbers 4, 5, 6, 7, 8, 9 but not 1, 2, 3.

I have tried using negative look behind with the regex (?<!^\w)\d+ but this matches the numbers in the first word.

Edit: Any numbers in the first continuous sequence of characters should not be matched, the first continuous sequence being from start (^) to a whitespace (\s). In 09B8A HE1LP only 1 should be matched, not 0, 9, or 8, as these digits are in the first word.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If your dialect supports variable-length negative lookbehinds, then this should work:

r = /(?<!^\w*)\d/g

console.log(...'A123 4HEL5P6 789'.match(r))

Otherwise, you could use /^\w*|\d/g and discard the first match.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading