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?
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.
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
([$€£]) *([0-9.,]*)
捕获组1将是货币符号,捕获组2将是金额,您还需要用Replace(',','')删除“,”
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);
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.