All Articles
Text

Match a Whole Number With Regex Without Matching Digits Inside Words

November 13, 20263 min read

Use \b\d+\b to find digit runs bounded by word boundaries in ordinary English text. In 42 A12B 3.14, it finds 42, 3 and 14 but does not match the digits inside A12B: it is not a decimal validator.

Regex tester with word boundaries and sample numbers
  1. Open the Regex Tester. Enter \b\d+\b in the pattern field.
  2. Leave g - Global checked and enter 42 A12B 3.14 in the test text. Inspect the highlighted matches and match list.
  3. Replace the pattern with \d+. The digit run 12 inside A12B now also matches.
Pattern: \b\d+\b
Text: 42 A12B 3.14
Matches: 42, 3, 14
Regex tester matching digits inside an ID

A word boundary is not a rule for whole numbers: the minus sign in -42 is outside the match, and decimal punctuation creates boundaries. Underscores and non-English identifiers also need tests suited to your data.

If it doesn't work

  • Enter the pattern without surrounding / delimiters in the field.
  • Check g - Global to find more than the first match.
  • For decimal or signed-number validation, define those rules separately and test negative cases.
RegexDeveloper Tools