![](/img/trans.png)
[英]regex to exact match 11 digit phone number from string and remove hyphen(-) from match in c#
[英]RegEx match to remove from string
當實例之前或之后沒有#
時,我需要從字符串中刪除#!#
實例。
例如...
LoremImpsum#!#Dolor
=> 匹配#!#
Lorem #!#ASD##
=> 匹配#!#
Lorem #!##
=> 不匹配
Lorem##!#
=> 不匹配
到目前為止我的代碼:
foreach (Match match in Regex.Matches(formattedHtml, @"(?<!#)(#!#)(?!#)")
formattedHtml = formattedHtml.Replace(match.Value, "");
但在我看來,負面的前瞻或后視是行不通的。 謝謝。
看起來您的代碼在多次出現的地方失敗,並且應該只替換其中一個。
你的正則表達式做了它應該做的。 但是,問題出在替換代碼上。 而不是跟隨
foreach (Match match in Regex.Matches(formattedHtml, @"(?<!#)(#!#)(?!#)")
formattedHtml = formattedHtml.Replace(match.Value, "");
你應該使用
formattedHtml = Regex.Replace(formattedHtml,@"(?<!#)(#!#)(?!#)","");
根據您的初始代碼,如果找到匹配項,它會替換字符串中的所有出現,即使它有前面/后面的 '#'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.