Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配表达式包含结束字_Regex - Fatal编程技术网

Regex 正则表达式匹配表达式包含结束字

Regex 正则表达式匹配表达式包含结束字,regex,Regex,String=“每年不得超过10%/适用的” 使每年的成为消费模式的一部分,因为当它在正向前瞻中时,匹配的文本不会添加到整个匹配值中 你可以用 string inputText = "shall not exceed 10 per cent. per annum/that applicable"; Regex re = new Regex("shall not exceed(.*?per annum)"); foreach (Match m in re.Matches(inputText)) {

String=“每年不得超过10%/适用的”


使每年的
成为消费模式的一部分,因为当它在正向前瞻中时,匹配的文本不会添加到整个匹配值中

你可以用

string inputText = "shall not exceed 10 per cent. per annum/that applicable";
Regex re = new Regex("shall not exceed(.*?per annum)");
foreach (Match m in re.Matches(inputText))
{
    Console.WriteLine(m.Groups[1].Value);
}

详细信息

  • 不得超过
    -文字字符串
  • (每年*)
    -捕获组1:
    • *?
      -0+字符(新行除外),尽可能少
    • 每年
      -文字字符串

第1组值可通过
m.Groups[1]访问。值

使
每年
成为消费模式的一部分-
(?删除前瞻
(每年)
@RAN_0915我也不认为捕获组是相关的:捕获固定字符串没有什么用处。@WiktorStribiżew-当我在regexr.com上尝试时效果很好,但当我们在asp.net regex代码上执行此操作时…代码如何工作…如果您想在上面包含“不得超过”在输出中需要更改什么?@user3657339这实际上是一个常见的逻辑问题:
Regex re=new Regex(“每年不得超过。*?”);
然后是
Console.WriteLine(m.Value);
。要跨行匹配,
“(?s)每年不得超过。*?”
            string regularExpressionPattern = regExPattern.ToString();
            string inputText = inputString
            Regex re = new Regex(regularExpressionPattern);
            foreach (Match m in re.Matches(inputText))
            {
                Response.Write(m.Groups[1].Value.ToString());
            }
string inputText = "shall not exceed 10 per cent. per annum/that applicable";
Regex re = new Regex("shall not exceed(.*?per annum)");
foreach (Match m in re.Matches(inputText))
{
    Console.WriteLine(m.Groups[1].Value);
}