[英]Regex to replace cash values in a string
我正在嘗試制作一個正則表達式來替換句子中的現金值,到目前為止,我正在使用此正則表達式[+-]?\\$\\s?((\\d{1,3},?)+.?\\d{2}?)\\b
。
以下是我想出了在測試用例test
是應該被捕獲並更換案件ntest
是FALS陽性。
public static void Main(string[] args)
{
string test = "$500, $5,000.00 $50.00 $.99 $80 -$500 $90!";
string output = Regex.Replace(test, @"[+-]?\$\s?((\d{1,3},?)+.?\d{2}?)\b", "money");
Console.WriteLine(output);
string ntest = "a$500, 00$5,000.00 $50.$00 $.99.00.00 $80.0000 --$90.00 !$90 ";
string output2 = Regex.Replace(ntest, @"[+-]?\$\s?((\d{1,3},?)+.?\d{2}?)\b", "money");
Console.WriteLine(output2);
}
到目前為止,它捕獲了大多數測試用例,但是我很難定義邊界。 結果:
money, money money $.99 $80 money $90!
amoneymoney $50.$00 $.99.00.00 $80.0000 -money !$90
結果實際上應該如下所示:
money, money money money money money money!
a$500, 00$5,000.00 $50.$00 $.99.00.00 $80.0000 --$90.00 !$90
我認為您正在尋找此正則表達式:
(?<=\s|^)\B[+-]?\$\s?\d{0,3}(?:,?\d{3})*(?:\.?\d{2})?\b(?=(?!\.)\p{P}|\s|$)
(或等效項: (?<=\\s|^)\\B[+-]?\\$\\s?\\d{0,3}(?:,?\\d{3})*(?:\\.?\\d{2})?\\b(?=[\\p{P}-[.]]|\\s|$)
)
觀看演示
不確定您要尋找的是什么,但是可以嘗試如下操作:
(?<=\s|\A)\B[+-]?\$\d{0,3}(?:,\d{3})*(?:\.\d{2})?\b(?=\s|\p{P}(?![\d$])|\z)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.