c# - Find out $ amount by using regular expressions -


can tell me how find out $ amount of below kind of string using regular expressions ? i'm not @ regex :( in advance.

jone has done $15 per unit: "good luck" 

result should = $15

you can use simple regex like

\$\d+ 
  • \$ matches $
  • \d+ matches 1 or more digits

regex demo

example

string str = "jone has done $15 per unit: \"good luck\""; regex pattern = new regex (@"\$\d+"); match match = pattern.match(str); console.writeline(match.groups[0]); => $15 

edit

to include decimals well

\$\d+(?:\.\d+)? 
  • \. matches decimal point
  • \d+ matches 1 or more digits
  • ? quantifier, matches 0 or 1 presceding pattern. quantifier ensure optional matching of decimal part.

regex demo

example

string str = "jone has done $15.5 per unit: \"good luck\""; regex pattern = new regex (@"\$\d+(?:\.\d+)?"); match match = pattern.match(str); console.writeline(match.groups[0]); => $15.5 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -