简体   繁体   中英

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

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

我通过在捕获组中包含“£”然后简单地用我正在使用的语言的匹配结果中的“无”替换该符号来做到这一点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM