.*和有什么不一样?和.*正则表达式? [英] What is the difference between .*? and .* regular expressions?

查看:76
本文介绍了.*和有什么不一样?和.*正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式将字符串分成两部分.字符串的格式如下:

I'm trying to split up a string into two parts using regex. The string is formatted as follows:

text to extract<number>

我一直在使用(.*?)<<(.*?)>,它们工作正常,但是在稍微读过正则表达式后,我开始怀疑为什么在表达式中需要?.在通过本网站找到他们之后,我才这样做,所以我不确定是否有什么区别.

I've been using (.*?)< and <(.*?)> which work fine but after reading into regex a little, I've just started to wonder why I need the ? in the expressions. I've only done it like that after finding them through this site so I'm not exactly sure what the difference is.

推荐答案

这是贪婪和非贪婪量词之间的区别.

It is the difference between greedy and non-greedy quantifiers.

考虑输入101000000000100.

使用1.*1时,*很贪婪-它将一直匹配到结尾,然后回溯直到可以匹配1,剩下1010000000001.
.*?是非贪婪的. *将不匹配任何内容,但随后将尝试匹配多余的字符,直到它匹配1,最终匹配101.

Using 1.*1, * is greedy - it will match all the way to the end, and then backtrack until it can match 1, leaving you with 1010000000001.
.*? is non-greedy. * will match nothing, but then will try to match extra characters until it matches 1, eventually matching 101.

所有量词都具有非贪婪模式:.*?.+?.{2,6}?甚至.??.

All quantifiers have a non-greedy mode: .*?, .+?, .{2,6}?, and even .??.

在您的情况下,类似的模式可能是<([^>]*)>-匹配大于号以外的任何字符(严格来说,它匹配<>之间的>以外的零个或多个字符)

In your case, a similar pattern could be <([^>]*)> - matching anything but a greater-than sign (strictly speaking, it matches zero or more characters other than > in-between < and >).

请参见 量化工具速查表 .

这篇关于.*和有什么不一样?和.*正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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