简体   繁体   中英

Regex to consider non-alphanumeric character other than $

I have a text :

Revenue: $12.9 billion
Regex=====>Revenue\W+(\$*\s*\d+\s*(\.\s*\d+)*)\s*billion

Now I want $12.9 in a specific group but by using \\W+ it takes ": $" after revenue. So I want to write such regex that it will consider all nonalphanumeric character except "$"

How can I do that???

A character class like [^\\w$] will match anything that is not a word character or $ . Also, there's no need to have \\s* twice together, or to have it appear after \\W+ (or [^\\w$]+ ) because the preceding character class will swallow up any whitespace characters anyway:

Revenue[^\w$]+(\d+\s*(\.\s*\d+))\s*billion
Revenue[^\w$]+(.\s*\d+\s*(\.\s*\d+))\s*billion

use this,

the . will match the unmatched $

demo here : http://regex101.com/r/aQ8qP7

$12.9 is captured in the first group

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