正则表达式:两个模式第一次出现之间的文本 [英] Regex: text between first occurrence of two patterns

查看:41
本文介绍了正则表达式:两个模式第一次出现之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/url?q=http://it.wikipedia.org/wiki/Spider-Man_(film)&sa=U&ei=iavVUKuFGsrNswbz74GQBA&ved=0CBYQFjAA&usg=AFQjCNEth5YspFPWp6CInyAfknlEvVg

/url?q=http://it.wikipedia.org/wiki/Spider-Man_(film)&sa=U&ei=iavVUKuFGsrNswbz74GQBA&ved=0CBYQFjAA&usg=AFQjCNEth5YspFPWp6CInyAfknlEvVgIfA

我只需要得到

http://it.wikipedia.org/wiki/Spider-Man_(电影)

我尝试使用 \?q=(.*)&但它考虑了 & 的最后一次出现,所以我得到

I tried with \?q=(.*)& but it consider last occurrence of &, so I get

http://it.wikipedia.org/wiki/Spider-Man_(film)&sa=U&ei=iavVUKuFGsrNswbz74GQBA&ved=0CBYQFjAA

http://rubular.com/r/yBiGIMQTUV

推荐答案

你需要使用reluctant matching来匹配到第一个&.使用贪婪匹配(即使用*而不是*?),您的模式将匹配尽可能长的字符串以满足完整的模式.

You need to use reluctant matching to match till the first &. With greedy matching (i.e. using * instead of *?), your pattern will match as long string as possible so as to satisfy the complete pattern.

所以使用这个:-

\?q=(.*?)&

或者你也可以使用带有否定 & 的字符类,它匹配除 & 之外的每个字符:-

Or you can also use character class with negated & which matches every character except &: -

\?q=([^&]*)

请注意,如果您不希望 (.*?) 匹配空字符串,那么您应该使用 + 量词.它匹配 1 或更多 次出现.

Note that, if you don't want your (.*?) to match empty string, then you should use + quantifier. It matches 1 or more occurrence.

这篇关于正则表达式:两个模式第一次出现之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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