简体   繁体   English

C# 正则表达式 - 只有在子字符串存在时才匹配?

[英]C# Regex - only match if substring exists?

Ok, so I think I've got a handle on negation - now what about only selecting a match that has a specified substring within it?好的,所以我想我已经掌握了否定的方法 - 现在只选择一个包含指定子字符串的匹配怎么办?

Given:鉴于:

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

And attempting the following regex:并尝试以下正则表达式:

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

Why isn't this working?为什么这不起作用? What am I missing?我错过了什么?

I'm using regexstorm.com for testing...我正在使用 regexstorm.com 进行测试...

You ruined a tempered greedy token by turning the negative lookahead into a positive one.你把消极的前瞻变成了积极的前瞻,这毁了一个温和的贪婪令牌。 It won't work that way because the positive lookahead requires the text to equal This is a random at each position after This is a random bit .它不会那样工作,因为正向前瞻要求文本等于This is a random at each position after This is a random bit

You need:你需要:

  • Match the leading delimiter ( This is a random bit )匹配前导分隔符( This is a random bit
  • Match all 0+ text that is not the leading/closing delimiters and not the required random text inside this block匹配所有 0+ 不是前导/结束定界符的文本,也不是此块内所需的随机文本
  • Match the specific string inside ( This is a random )匹配里面的特定字符串( This is a random
  • Match all 0+ text that is not the leading/closing delimiters匹配所有 0+ 不是前导/结束定界符的文本
  • Match the closing delimiter ( This is the end )匹配结束分隔符( This is the end

So, use所以,使用

(?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

See the regex demo查看正则表达式演示

  • (?s) - DOTALL mode on ( . matches a newline) (?s) - DOTALL 模式 ( .匹配换行符)
  • This is a random bit - Leading delimiter 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 - specified substring This is a random指定的子串
  • (?:(?!This is a random bit|This is the end).)* - Another tempered greedy token matching any text not leading/closing delimiters up to the first... (?:(?!This is a random bit|This is the end).)* - 另一个缓和的贪婪标记匹配任何文本,直到第一个前/结束分隔符...
  • This is the end - trailing delimiter This is the end尾随定界符

I hope you understand this (?:(?=This is a random).) can only match once, never twice if it were quantified.我希望你明白这一点(?:(?=This is a random).)只能匹配一次,如果被量化则永远不会匹配两次。 For example Th can satisfy the lookahead.例如Th可以满足前瞻。 When the T is consumed, the next character is h which will never satisfy the lookahhead Th .T被消耗时,下一个字符是h ,它永远不会满足 Lookahhead Th The next expression is evaluated, never to return to the lookahead again.评估下一个表达式,永远不会再次返回前瞻。 Use a negative lookahead instead.改用负前瞻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM