正则表达式中的问号 [英] Question marks in regular expressions
问题描述
我正在阅读正则表达式参考,我在想?和 ??人物.你能用一些例子向我解释它们的用处吗?我不太了解它们.
I'm reading the regular expressions reference and I'm thinking about ? and ?? characters. Could you explain me with some examples their usefulness? I don't understand them enough.
谢谢
推荐答案
?
和 ??
之间的主要区别在于他们的懒惰.??
是懒惰的,?
不是.
The key difference between ?
and ??
concerns their laziness. ??
is lazy, ?
is not.
假设您想在文本正文中搜索单词car",但您不想仅限于单数的car";您还想匹配复数汽车".
Let's say you want to search for the word "car" in a body of text, but you don't want to be restricted to just the singular "car"; you also want to match against the plural "cars".
这是一个例句:
我拥有三辆车.
现在,如果我想匹配单词car"而我只想得到字符串car"作为回报,我会使用懒惰的??
像这样:
Now, if I wanted to match the word "car" and I only wanted to get the string "car" in return, I would use the lazy ??
like so:
汽车??
这表示,寻找汽车或汽车这个词;如果找到,则返回car
,仅此而已".
This says, "look for the word car or cars; if you find either, return car
and nothing more".
现在,如果我想匹配相同的词(car"或cars")并且我想得到整个匹配的回报,我会使用非懒惰的 <代码>? 像这样:
Now, if I wanted to match against the same words ("car" or "cars") and I wanted to get the whole match in return, I'd use the non-lazy ?
like so:
汽车?
这表示,查找汽车或汽车这个词,然后返回汽车或汽车,无论您找到什么".
This says, "look for the word car or cars, and return either car or cars, whatever you find".
在计算机编程的世界中,懒惰通常意味着仅根据需要进行评估".所以懒惰的 ??
只返回匹配所需的数量;由于cars"中的s"是可选的,所以不要返回它.另一方面,非惰性(有时称为 greedy)操作会尽可能多地求值,因此 ?
返回所有匹配项,包括可选的s".
In the world of computer programming, lazy generally means "evaluating only as much as is needed". So the lazy ??
only returns as much as is needed to make a match; since the "s" in "cars" is optional, don't return it. On the flip side, non-lazy (sometimes called greedy) operations evaluate as much as possible, hence the ?
returns all of the match, including the optional "s".
就我个人而言,我发现自己使用 ?
作为一种使其他正则表达式运算符变得懒惰的方式(例如 *
和 +
运算符)更多通常比我将它用于简单的字符可选性,但是 YMMV.
Personally, I find myself using ?
as a way of making other regular expression operators lazy (like the *
and +
operators) more often than I use it for simple character optionality, but YMMV.
以上以Clojure实现为例:
Here's the above implemented in Clojure as an example:
(re-find #"cars??" "I own three cars.")
;=> "car"
(re-find #"cars?" "I own three cars.")
;=> "cars"
项目 re-find
是一个函数,它将其第一个参数作为正则表达式 #"cars??"
并返回它在第二个中找到的第一个匹配项参数 我拥有三辆车."
The item re-find
is a function that takes its first argument as a regular expression #"cars??"
and returns the first match it finds in the second argument "I own three cars."
这篇关于正则表达式中的问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!