以下正则表达式之间有什么区别
Write(?:Line)?
和
Write(Line)?
我问的是:
International
的以下变体形式相匹配:Int,Tntl,International
答案 0 :(得分:4)
?:
组是非捕获组,意味着它不会包含在结果中。
//Will match a "WriteLine" or "Write", but will ignore the Line in the result
Write(?:Line)?
//*match* -> *captured as*
//WriteLine -> Write
//Write -> Write
//Will match a "WriteLine" or "Write"
Write(Line)?
//*match* -> *captured as*
//WriteLine -> WriteLine
//Write -> Write
#2
的正则表达式如果我不理解,请纠正我。
如果您想将Int
或Tntl
替换为International
,请执行以下操作:
var result = Regex.Replace("International:Int,Tntl,International","(Int(ernational)?|Tntl)","International");
// "International:Int,Tntl,International" ->
// "International:International,International,International"
管道符号|
充当正则表达式的or
运算符。
(International|Int|Tntl)