如果找不到给定的文本,则匹配正则表达式,并尽可能少地匹配 [英] Regex to match if given text is not found and match as little as possible

查看:117
本文介绍了如果找不到给定的文本,则匹配正则表达式,并尽可能少地匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文字:

<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>
<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>
<a>
sdfsdf
<b>BBzz</b>
sdfsdf
</a>
<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>

我无法将其解析为xml.我需要在这里使用正则表达式.但这只是一个例子.

I can't parse it as xml. I need to use regex here. Also this is only example.

我希望正则表达式能够将不包含元素b的每个组<a>...</a>与以BB开头的文本进行匹配.

I want regex that can match every group <a>...</a> that does not contain element b with text that starts with BB.

我想出了这个正则表达式: <a>.*?<b>(?!B).*?</b>.*?</a> 但它匹配的最后一组为:

I came up with this regex: <a>.*?<b>(?!B).*?</b>.*?</a> But it matches last group as:

<a>
sdfsdf
<b>BBzz</b>
sdfsdf
</a>
<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>

哪个对我有害?

如何编写仅与给定示例中的那3组匹配的正则表达式?:

1.

<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>

2.

<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>

3.

<a>
sdfsdf
<b>DDzz</b>
sdfsdf
</a>

推荐答案

使用脾气暴躁的令牌正则表达式:

Use a tempered greedy token regex:

<a>(?:(?!<(?:b>BB|/?a>)).)*</a>

启用 .匹配换行符选项.

Enable the . matches newline option.

详细信息:

  • <a>-文字<a>字符序列
  • (?:(?!<(?:b>BB|/?a>)).)*-与任何字符(.)匹配的脾气暴躁的令牌,该字符不是可以与(?!<(?:b>BB|/?a>))前瞻(不是<b>BB<a>序列)
  • </a>-文字</a>字符序列
  • <a> - a literal <a> char sequence
  • (?:(?!<(?:b>BB|/?a>)).)* - a tempered greedy token matching any char (.) that is not the starting symbol of a sequence that can be matched with the pattern inside the (?!<(?:b>BB|/?a>)) lookahead (not a <b>BB or </a> or <a> sequence)
  • </a> - a literal </a> char sequence

这篇关于如果找不到给定的文本,则匹配正则表达式,并尽可能少地匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆