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
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.
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