general exception
general exception

Reputation: 4332

Regex match but exclude character from capture group

I have the following regex:-

^(?:\[ [^\]]+ \]) ([\-\£]{1,2}[0-9]+\.[0-9][0-9]?)?

And the following text:-

John Doe
+++++++++++++++++++++++++++++++++++++++++++
Blah blah
+++++++++++++++++++++++++++++++++++++++++++
[ √ ] £3.00 Red
[ √ ] £2.00 Blue
[ √ ] £55.55 Yellow
[ √ ] -£41.75 PAYMENT
[ √ ] £5.00 Green
[ √R ] £10.00 Pink
+++++++++++++++++++++++++++++++++++++++++++
Belugh
+++++++++++++++++++++++++++++++++++++++++++
Blah blah
Blah

The regex works ok and matches:-

£3.00
£2.00
£55.55
-£41.75
£5.00
£10.00

What I want to do though is to have the regex match the '£' but not return it in the capturing group.

So the results would be like this:-

3.00
2.00
55.55
-41.75
5.00
10.00

The important bit is the '-' character which should remain, but occurs before the '£' character.

Theres a fiddle here

Upvotes: 6

Views: 17734

Answers (2)

Eddie Davidson
Eddie Davidson

Reputation: 19

I did this by including the "£" in the capture group and then simply replacing that symbol with "nothing" from the matched results in the language I'm working in.

Upvotes: 0

asontu
asontu

Reputation: 4649

Best you can do is this:

^(?:\[ [^\]]+ \]) (-?)[\£]([0-9]+\.[0-9][0-9]?)?

And then extract group 1 and group 2 from the matches and append them in the language/environment you're working in.

Demo

Upvotes: 5

Related Questions