1

I have a text with a number that contains dots:

text 304.33.44.52.03.001 text

where I want to capture the number including strings:

304.33.44.52.03.001

The following regex will capture sevaral groups:

(\d+\.?)

Resulting in:

304.
33.
44.
...

What is the correct syntax to return the entire number including dots in one result?

merlin
  • 2,717
  • 3
  • 29
  • 59

1 Answers1

1

\d+\.? matches 1+ digits and then an optional . char.

You need to use either

\d+(?:\.\d+)*

or

\d[\d.]*

See the regex demo

The \d+(?:\.\d+)* pattern matches

  • \d+ - 1+ digits
  • (?:\.\d+)* - 0 or more occurrences of a . and then 1+ digits. (?:...) is a non-capturing group that is used to group 2 patterns and set a quantifier on their sequence.

The \d[\d.]* pattern matches a digit first, and then tries to match 0 or more digits or ..

In regex engines that do not support \d you need to use a safer pattern, a bracket expression [0-9].

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563