Khaine775
Khaine775

Reputation: 2765

Regex capture decimal within a string

So I've got a string consisting of a mixture of text, special characters and decimals:

2018-12-18 00:00:00 : TEXT0        TEXT1 0,123 - TEXT2 1,123 - TEXT3 2,123 - TEXT4 3,123

I'm interested in something that can capture 2,123. So far I've tried something like:

(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) : (V04\.10\w+|\w+ ) \w+ \d+,\d{3} - \w+ \d+,d{3} - \w+ (\d+,d{3}) - \w+ \d+,d{3}

But it does not seem to work. I'm capturing two more groups here, one for the date in the front and one for TEXT0 as well. They work fine by themselves. Any hints?

I'm sitting in .NET 4.5.2.

Upvotes: 0

Views: 40

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

Besides writing d instead of \d and matching a single whitespace, you might shorten your regex a bit by repeating the part that matches \w+ \d,\d{3} - and make the digit part with the comma a capturing group.

That way the capturing group will be the last occurence of the repeated pattern:

(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) : (V04\.10\w+|\w+ +)(?:\w+ (\d,\d{3}) - )*\w+ \d+,\d{3}

Regex demo

Upvotes: 1

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

Seems you just had few typos in your regex where you wrote only d instead of \d for intending to capture number and at one place, there were multiple spaces but you just wrote single space due to which it didn't match.

Change your regex from this,

(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) : (V04\.10\w+|\w+ ) \w+ \d+,\d{3} - \w+ \d+,d{3} - \w+ (\d+,d{3}) - \w+ \d+,d{3}

to this,

(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) : (V04\.10\w+|\w+) +\w+ \d+,\d{3} - \w+ \d+,\d{3} - \w+ (\d+,\d{3}) - \w+ \d+,\d{3}

And it will start matching and capturing the data as you expect.

Live Demo

Upvotes: 1

Related Questions