Bent
Bent

Reputation: 1385

Regex how to match two similar numbers in separate match groups?

I got the following string:

[13:49:38 INFO]: Overall : Mean tick time: 4.126 ms. Mean TPS: 20.000

the bold numbers should be matched, each into its own capture group.

My current expression is (\d+.\d{3}) which matches 4.126 how can I match my 20.000 now into a second capture group? Adding the same capture group again makes it find nothing. So what I basically need is, "search for first number, then ignore everything until you find next digit."

Upvotes: 0

Views: 115

Answers (1)

npinti
npinti

Reputation: 52185

You could use something like so: (\d+\.\d{3}).+?(\d+\.\d{3})$ (example here) which essentially is your regex (plus a minor fix) twice, with the difference that it will also look for the same pattern again at the end of the string.

Another minor note, your regex contains, a potential issue in which you are matching the decimal point with the period character. In regular expression language, the period character means any character, thus your expression would also match 4s222. Adding an extra \ in front makes the regex engine treat is as an actual character, and not a special one.

Upvotes: 1

Related Questions