Reputation: 2199
I am trying to create a regex for the following outputs:
Text - Output Expected
$200 currency sign = "$" and amount = 200
€ 20.34 currency sign = "€" and amount = 20.34
£ 190,234 currency sign = "£" and amount = 190234
$ 20.34 currency sign = "$" and amount = 20.34
I am not good with regex but still I want to do this with regex. Can anybody help me to achieve this?
Upvotes: 0
Views: 10897
Reputation: 9
string str = string.Empty , outPut = string.Empty;
Regex paternWithDot = new Regex(@"\d+(\.\d+)+");
Regex paternWithComa = new Regex(@"\d+(\,\d+)+");
Match match = null;
str = "& 34.34";
if (str.Contains(".")) match = paternWithDot.Match(str);
if (str.Contains(",")) match = paternWithComa.Match(str);
outPut += string.Format( @" currency sign = ""{0}"" and amount = {1}" , str.Replace(match.Value, string.Empty), match.Value) + Environment.NewLine;
str = "€ 20.34 ";
if (str.Contains(".")) match = paternWithDot.Match(str);
if (str.Contains(",")) match = paternWithComa.Match(str);
outPut += string.Format(@" currency sign = ""{0}"" and amount = {1}", str.Replace(match.Value, string.Empty), match.Value) + Environment.NewLine;
str = "£ 190,234";
if (str.Contains(".")) match = paternWithDot.Match(str);
if (str.Contains(",")) match = paternWithComa.Match(str);
outPut += string.Format(@" currency sign = ""{0}"" and amount = {1}", str.Replace(match.Value, string.Empty), match.Value) + Environment.NewLine;
str = "$ 20.34 ";
if (str.Contains(".")) match = paternWithDot.Match(str);
if (str.Contains(",")) match = paternWithComa.Match(str);
outPut += string.Format(@" currency sign = ""{0}"" and amount = {1}", str.Replace(match.Value, string.Empty), match.Value) + Environment.NewLine;
MessageBox.Show(outPut);
Upvotes: 0
Reputation: 26667
You can use the regex:
(\D)\s*([.\d,]+)
the caputre group 1 will contain currency symbol and group 2 contians value
see the demo http://regex101.com/r/eV2uZ7/1
EXPLANTION
(\D)
mathces anything other than digit
\s*
matches any number of spaces.
[.\d,]+
matches digits, comma and dot.
To be more specific you can also give \d[.\d,]*
which ensures that the value part always begin with a digit
Upvotes: 3
Reputation: 274
You can use this regex to capture symbol and amout :
/(?<SYMBOL>[$€£]){1}\s*(?<AMOUNT>[\d.,]+)/g
DEMO (Look at the match information on the right panel)
Hope it helps.
Upvotes: 4
Reputation: 1132
([$€£]) *([0-9.,]*)
Capture group 1 will be the currency symbol, capture group 2 will be the amount also you will need to remove "," after that with Replace(',','')
Upvotes: 0