本文翻译自:How can I write a regex which matches non greedy? [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • How do I match any character across multiple lines in a regular expression? 如何在正则表达式中跨多行匹配任何字符? 23 answers 23个答案

I need help about regular expression matching with non-greedy option. 我需要有关使用非贪婪选项进行正则表达式匹配的帮助。

The match pattern is: 匹配模式为:

<img\s.*>

The text to match is: 要匹配的文本是:

<html>
<img src="test">
abc
<imgsrc="a" src='a' a=b>
</html>

I test on http://regexpal.com 我在http://regexpal.com上测试

This expression matches all text from <img to last > . 此表达式匹配从<img到last >所有文本。 I need it to match with the first encountered > after the initial <img , so here I'd need to get two matches instead of the one that I get. 我需要它与初始<img之后的第一个遇到的>匹配,因此在这里,我需要得到两个匹配项,而不是我得到的匹配项。

I tried all combinations of non-greedy ? 我尝试了所有非贪婪的组合? , with no success. ,但没有成功。


#1楼

参考:https://stackoom.com/question/nvTK/如何编写与非贪婪匹配的正则表达式-重复


#2楼

The non-greedy ? 不贪心? works perfectly fine. 工作完美。 It's just that you need to select dot matches all option in the regex engines ( regexpal , the engine you used, also has this option) you are testing with. 只需在要测试的正则表达式引擎中选择点匹配所有选项( regexpal ,您使用的引擎,也具有此选项)。 This is because, regex engines generally don't match line breaks when you use . 这是因为,使用时,正则表达式引擎通常与换行符不匹配. . You need to tell them explicitly that you want to match line-breaks too with . 您需要明确告知他们您也想将换行符与匹配.

For example, 例如,

<img\s.*?>

works fine! 工作正常!

Check the results here . 在这里检查结果 。

Also, read about how dot behaves in various regex flavours. 另外,请阅读有关点在各种正则表达式中的行为方式的信息 。


#3楼

The ? ? operand makes match non-greedy. 操作数使匹配非贪婪。 Eg .* is greedy while .*? 例如.*是贪婪的,而.*? isn't. 不是。 So you can use something like <img.*?> to match the whole tag. 因此,您可以使用<img.*?>来匹配整个标签。 Or <img[^>]*> . <img[^>]*>

But remember that the whole set of HTML can't be actually parsed with regular expressions. 但是请记住,实际上无法使用正则表达式来解析整个HTML集合。


#4楼

The other answers here presuppose that you have a regex engine which supports non-greedy matching, which is an extension introduced in Perl 5 and widely copied to other modern languages; 这里的其他答案以您有一个支持非贪婪匹配的正则表达式引擎为前提,该引擎是Perl 5中引入的扩展,并且已广泛复制到其他现代语言中。 but it is by no means ubiquitous. 但这绝不是普遍存在的。

Many older or more conservative languages and editors only support traditional regular expressions, which have no mechanism for controlling greediness of the repetition operator * - it always matches the longest possible string. 许多较早或更保守的语言和编辑器仅支持传统的正则表达式,而后者没有控制重复操作符* -它始终与最长的字符串匹配。

The trick then is to limit what it's allowed to match in the first place. 然后,诀窍是首先限制允许匹配的内容。 Instead of .* you seem to be looking for 而不是.*您似乎正在寻找

[^>]*

which still matches as many of something as possible; 仍然匹配尽可能多的东西 but the something is not just . 但是事情不仅仅是. "any character", but instead "any character which isn't > ". “任何字符”,而是“不是>任何字符”。

Depending on your application, you may or may not want to enable an option to permit "any character" to include newlines. 根据您的应用程序,您可能会或可能不希望启用允许“任何字符”包括换行符的选项。

Even if your regular expression engine supports non-greedy matching, it's better to spell out what you actually mean. 即使您的正则表达式引擎支持非贪婪匹配,也最好阐明您的实际意思。 If this is what you mean, you should probably say this, instead of rely on non-greedy matching to (hopefully, probably) Do What I Mean. 如果这您的意思,那么您可能应该这样说,而不是依靠非贪婪的匹配来(希望是,也许是)做到我的意思。

For example, a regular expression with a trailing context after the wildcard like .*?><br/> will jump over any nested > until it finds the trailing context (here, ><br/> ) even if that requires straddling multiple > instances and newlines if you let it, where [^>]*><br/> (or even [^\\n>]*><br/> if you have to explicitly disallow newline) obviously can't and won't do that. 例如,正则表达式中包含通配符后尾随背景.*?><br/>将跳过任何嵌套>直到找到尾随上下文(在这里, ><br/> ),即使需要跨越多个>实例和换行符(如果允许的话),显然, [^>]*><br/> (甚至如果必须明确禁止换行符,甚至是[^\\n>]*><br/> )也不会去做。

Of course, this is still not what you want if you need to cope with <img title="quoted string with > in it" src="other attributes"> and perhaps <img title="nested tags"> , but at that point, you should finally give up on using regular expressions for this like we all told you in the first place. 当然,如果您需要处理<img title="quoted string with > in it" src="other attributes"> and perhaps <img title="nested tags"> ,那么这仍然不是您想要的,但是一点,您最终应该放弃使用正则表达式,就像我们一开始就告诉您的那样。

如何编写与非贪婪匹配的正则表达式? [重复]相关推荐

  1. 匹配正则_程序员入门基础:python正则表达式贪婪匹配和非贪婪匹配

    此文为python正则表达式的高阶入门,正则基础入门请参考程序员入门基础:python的正则表达式. 一.贪婪匹配和非贪婪匹配 举例说明概念: print('非贪婪匹配',re.search('el+ ...

  2. 正则表达式:贪婪匹配与非贪婪匹配

    情景 之前写过一个简单的爬虫,每天获取公司insgtagram主页的粉丝数用来进行粉丝趋势的展示.代码很简单就是通过获取主页源代码后用正则表达式匹配其中的一串json数据,再用python的json解 ...

  3. VIM 用正则表达式,非贪婪匹配,匹配竖杠,竖线, 匹配中文,倒数第二列, 匹配任意一个字符 :...

    VIM 用正则表达式 批量替换文本,多行删除,复制,移动 在VIM中 用正则表达式 批量替换文本,多行删除,复制,移动 :n1,n2 m n3     移动n1-n2行(包括n1,n2)到n3行之下: ...

  4. python中正则表达式的默认匹配方式为贪婪匹配_python正则表达式贪婪算法与非贪婪算法与正则表达式子模式的简单应用...

    先引入一下百度百科对于正则表达式的概念: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则 ...

  5. 正则表达式规则以及贪婪匹配与非贪婪匹配

    1.什么是正则表达式的贪婪与非贪婪匹配 如: String str="abcaxc";Patter p="ab*c"; 贪婪匹配:正则表达式一般趋向于最大长度匹 ...

  6. 10、正则表达式 (笔试题、语法规则、正则对象方法、正则实例属性、支持正则表达式的String对象的方法、贪婪匹配与非贪婪匹配)

    正则表达式 目录 10.1 语法规则 10.1.1 创建方法 1.直接量 2.构造方法RegExp() 10.1.2 三个属性i,g,m 10.1.3 方括号 10.1.4 元字符 10.1.5 量词 ...

  7. 正则表达式之贪婪匹配与非贪婪匹配

    在前面正则表达式匹配规则里,提到了 .* . :匹配除 "\n" 之外的任何单个字符.要匹配包括 '\n' 在内的任何字符,请使用像 '[.\n]' 的模式 * :匹配0个或多个 ...

  8. 正则表达式——非贪婪匹配

    在介绍非贪婪匹配前,我们先看一个简单的问题: 给定一个字符串表示的数字,判断该数字末尾0的个数.例如: "123000":3个0 "10100":2个0 &qu ...

  9. python正则表达式贪婪匹配_Python正则表达式教程之三:贪婪/非贪婪特性

    之前已经简单介绍了Python正则表达式的基础与捕获,那么在这一篇文章里,我将总结一下正则表达式的贪婪/非贪婪特性. 贪婪 默认情况下,正则表达式将进行贪婪匹配.所谓"贪婪",其实 ...

最新文章

  1. 华尔街风暴的深层原因
  2. python 测试用例
  3. 用java实现验证码(CAPTCHA)
  4. 信息收集——Office钓鱼
  5. python的逆袭之路_Python领域最伟大工程师Kenneth Reitz的逆袭之路
  6. React Native在美团外卖客户端的实践
  7. 字符串的存储方式以及静态存储区域、栈、堆
  8. Linux(Centos7)下使用RPM方式安装MySQL5.7
  9. 简单的webservice发布和测试
  10. sqlloader 直接路径和常规路径_sqlloader
  11. CH340G版USB转串口自动下载器原理图
  12. 逆向、反编译、微信相关记录
  13. REST Assured 4 - 第一个GET Request
  14. HTML5期末大作业:仿悦世界游戏网站设计——仿悦世界游戏官网(6页) HTML+CSS+JavaScript web网页设计实例作业
  15. Linux服务器期末复习总结
  16. 乒乓球单循环赛_【乒乓球比赛单循环表资讯】乒乓球比赛单循环表足球知识与常识 - 足球百科 - 599比分...
  17. 第十二章 牛市股票还会亏钱-外观模式(读书笔记)
  18. c语言float代码,[求助]float
  19. 【IDE】Eclipse中的workspace workingset 如何更好的搭配使用,以及本地项目文件夹的分类
  20. 什么叫蓝筹股?什么叫蓝筹股票的特点

热门文章

  1. 让你的容器兼容STL
  2. 为.net中的ListBox控件添加双击事件
  3. Android Handler 流程解析
  4. Android Studio自定义模板代码
  5. git上传自己的代码
  6. java8 metaspacesize_metaspace默认大小
  7. java 字符串乱码_这份Java面试题含答案解析竟然真的让你不用在面试上“如履薄冰”...
  8. 创建型模式--原型模式
  9. Android刷新机制-View绘制原理
  10. 【Webview相关问题】控制字符引发的惨案及Base64