Reputation: 69
How can I improve this regular expression?
(((\s|^)(\d+)*(\s|$))|((\s|^)(\d{1,3})(\.\d{3})*(\s|$)))
I want to recognise only the numbers from a string. The numbers must be without anything, or if it has dot must be grouped in groups of three but must be taken as a whole number. the number can begin with whitespace or to be at the begining of the line and can end with whitespace or end of the line.
Currently if the string beggins with whitespace it will find only the whitespace.
Upvotes: 2
Views: 89
Reputation: 59
How about something like that
((\s\d+\s)|(\s*\d{1,3}(\.\d{3})+\s*))
The first part (\s\d+\s)
matches solo numbers, the second (\s*\d{3}\.\d{3}\s*)
matches numbers with dots and groups of three.
I dont think that you need to declare the beginning or end of a line in the RegEx
Edit:
that might be even easier
(\s*\d(\.\d{3})*\s*)
You look for a number, and optional some groups of .\d\d\d
Upvotes: 0
Reputation: 627517
You may use
(?<![\d.])\b(?:\d{1,3}(?:\.\d{3})*|\d+)\b(?!\.\d)
See the regex demo
Details:
(?<![\d.])
- there should be no digit or dot immediately before the current position\b
- a leading word boundary, there must be start of string or a non-word char before the current position(?:\d{1,3}(?:\.\d{3})*|\d+)
- 1 of the 2 alternatives:
\d{1,3}(?:\.\d{3})*
- 1 to 3 digits followed with zero or more sequences of .
and 3 digits|
- or \d+
- one or more digits\b
- a trailing word boundary(?!\.\d)
- there must be no dot and a digit immediately after the current position.Upvotes: 1