C#Regex - 仅在子字符串存在时才匹配?

时间:2016-05-01 21:12:35

标签: c# regex regex-lookarounds lookahead

好的,所以我认为我已经掌握了否定 - 那么现在只选择一个具有指定子字符串的匹配呢?

假设:

This is a random bit of information from 0 to 1.
  This is a non-random bit of information I do NOT want to match
This is the end of this bit

This is a random bit of information from 0 to 1.
  This is a random bit of information I do want to match
This is the end of this bit

尝试以下正则表达式:

/(?s)This is a random bit(?:(?=This is a random).)*?This is the end/g

为什么这不起作用?我错过了什么?

我使用regexstorm.com进行测试......

2 个答案:

答案 0 :(得分:2)

通过将负向前瞻转变为积极的前瞻,你毁掉了一个顽固的贪婪的象征。它不会那样工作,因为正向前瞻要求文本在This is a random之后的每个位置等于This is a random bit

你需要:

  • 匹配前导分隔符(This is a random bit
  • 匹配不是前导/结束分隔符的所有0+文本,而不是此块中所需的随机文本
  • 匹配(This is a random
  • 内的特定字符串
  • 匹配非领先/结束分隔符的所有0+文本
  • 匹配结算分隔符(This is the end

所以,使用

(?s)This is a random bit(?:(?!This is a random bit|This is the end|This is a random).)*This is a random(?:(?!This is a random bit|This is the end).)*This is the end

请参阅regex demo

  • (?s) - DOTALL模式开启(.匹配换行符)
  • This is a random bit - 领导分隔符
  • (?: # Start of the tempered greedy token (?!This is a random bit # Leading delimiter | This is the end # Trailing delimiter | This is a random) # Sepcific string inside . # Any character )* # End of tempered greedy token
  • This is a random - 指定子字符串
  • (?:(?!This is a random bit|This is the end).)* - 另一个顽固的贪婪令牌,匹配任何不领先/关闭分隔符的文本到第一个...
  • This is the end - 尾随分隔符

答案 1 :(得分:0)

我希望你明白这个(?:(?=This is a random).)只能匹配一次,如果量化则永远不会两次。例如,Th可以满足前瞻。消耗T时,下一个字符h将永远不会满足前瞻Th。评估下一个表达式,永远不再返回到前瞻。改为使用负向前瞻。