x Hint: Register, if you want to hide ads.
x Did you like it? Well, then please consider making a donation :)

Regex Tag - numbers

How do I replace multiple spaces with a single space?

Replace multiple spaces example

Type: replace, Date: 7/12/2015 4:10:18 PMAuthor: stackoverflow

Validate numbers

If Possible, Don't Use Regex To Validate Numbers. If at all possible, use your language. There may be functions to help you determine if the value contained by a string is a valid number. That being said, if you're accepting a variety of formats (commas, etc.) you may not have a choice.

Type: match, Date: 7/12/2015 4:07:04 PMAuthor: stackoverflow

Get only Arabic digits

Use ([0-9]+) if you need to match more than one digit (only Arabic numbers!). Use (\d+) if you need all digits (all unicode digits).

Type: match, Date: 7/12/2015 4:00:33 PMAuthor: stackoverflow

How to get numbers 1st, 2nd, 3rd, 4th from text

You can use capturing groups to organize and parse an expression. A non-capturing group has the first benefit, but doesn't have the overhead of the second. You can still say a non-capturing group is optional, for example. Say you want to match numeric text, but some numbers could be written as 1st, 2nd, 3rd, 4th... If you want to capture the numeric part, but not the (optional) suffix you can use a non-capturing group. ([0-9]+)(?:st|nd|rd|th)? That will match numbers in the form 1, 2, 3... or in the form 1st, 2nd, 3rd,... but it will only capture the numeric part.

Type: match, Date: 7/12/2015 3:26:19 PMAuthor: stackoverflow