Matching number segments using regex
Creating classifications for numbers using only regex.
The below example can be used to segment time in milliseconds into appropriate time groupings;
\b(?:[0-9][0-9]?[0-9]?)\b // < 1 second
\b(?:[1-2][0-9][0-9][0-9])\b // 1-3 seconds
\b(?:[3-4][0-9][0-9][0-9])\b // 3-5 seconds
\b(?:[1-9][0-9][0-9][0-9][0-9]*|[5-9][0-9][0-9])\d\b // > 5 seconds
What the regex means:
- \b - Word boundary
- (?: - Non capturing group: groups multiple tokens together without creating a capture group.
- [0-9] - Character Set: we use one for each character we expect to see.
- ? - Quantfier: used to allow 0 or 1 or preceding tokens
-
The basis for this regex is the following syntax, here matching the range 0-9
\b[0-9]\b
-
This can then be slowly build up to match your desired range. e.g. 1-100
\b([0-9]|[1-9][0-9]|100)\b