0

I want to extract an ID from a search query but I don't know the length of the ID.

From this input I want to get the numbers that are not in the words and the numbers that are not separated by symbols.

12 11231390 good123e41 12he12o1 1391389 dajue1290a 12331 12-10 1.2 test12.0why 12+12 12*6 2d1139013 09`29 83919 1

Here I want to return

12 11231390 1391389 12331 83919 1

So far I've tried /\b[^\D]\d*[^\D]\b/gm but I get the numbers in between the symbols and I don't get the 1 at the end.

JBBaZkMV
  • 45
  • 2

1 Answers1

4

You could repeatedly match digits between whitespace boundaries. Using a word boundary \b would give you partial matches.

Note that [^\D] is the same as \d and would expect at least a single character.

Your pattern can be written as \b\d\d*\d\b and you can see that you don't get the 1 at the end as your pattern matches at least 2 digits.

(?<!\S)\d+(?:\s+\d+)*(?!\S)

The pattern matches:

  • (?<!\S) Negateive lookbehind, assert a whitespace boundary to the left
  • \d+(?:\s+\d+)* Match 1+ digits and optionally repeat matching 1+ whitespace chars and 1+ digits.
  • (?!\S) Negative lookahead, assert a whitspace boundary to the right

Regex demo

If lookarounds are not supported, you could use a match with a capture group

(?:^|\s)(\d+(?:\s+\d+)*)(?:$|\s)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70