Skip to main content
 首页 » 编程设计

C# 的正则表达式不能像我想要的那样工作

2024年01月25日27zhoujg

我有一个这样的输入字符串:

\pard\nowidctlpar\qj\b0\scaps This Agreement\scaps0 is made and entered into this date, \{#DATEAGREEMENT#\} , by and between [removed] (\ldblquote [removed]\rdblquote ), and its successors and/or assigns, with address at [removed], and \{#TXTPLAINTIFF#\} , (\ldblquote Plaintiff\rdblquote ), Individually, of ___________________, and, \{#TXTPLAINTIFFATTORNEY#\} , (\ldblquote Plaintiff\rquote s Attorney\rdblquote ) of \{#TXTATTORNEYFIRM#\} .\par

我想用正则表达式做一些解析。

这是我正在使用的代码:

temp = Regex.Replace(temp, @"\\{#.*#\\}", "_____"); 

唯一的问题是它匹配最宽的一组,即它取出整个段落,因为段落以“#\}”结尾,我怎样才能匹配每个组?也就是说,这段应该匹配 4 次而不是一次。

请您参考如下方法:

您是否尝试过使其成为非贪婪匹配?

temp = Regex.Replace(temp, @"\\{#.*?#\\}", "_____");