正则表达式忽略问号符号后的所有内容 [英] Regexp that ignores everything after a question mark symbol
问题描述
这是我要使用正则表达式解析的完整字符串:
This is the full string I'm trying to parse with regular expressions:
example.com/newsite.com.html?var=/newsite.com&var=newsite.com
我希望能够匹配字符串的newsite.com
部分,但前提是该字符串不出现在?
符号之后.
I would like to be able to match newsite.com
part of the string but only if it does not appear after the ?
symbol.
目前,我到现在为止:
/newsite.com/g
选择newsite.com
的所有实例,而不是第一个.
Which selects all the instances of newsite.com
instead of just the first one.
链接到正则表达式游乐场 http://regexr.com/3fmre
Link to the regexp playground http://regexr.com/3fmre
这里*
表示我想忽略的所有内容,基本上只匹配第一次出现的newsite.com
:
Here *
represents everything I would like to ignore, essentially matching only the first occurrence of newsite.com
:
example.com/newsite.com.html?****************************
推荐答案
这是我想出的解决方案,它不是防弹的,但在大多数情况下都有效:
This is the solution I came up with, it's not bullet-proof but works for the most part:
(?<=[^=]\/)newsite\.com
它将忽略以=
开头且开头没有/
的任何内容.
It ignores anything that starts with =
and doesn't have a /
in the beginning.
请注意,这仅适用于支持正向后看"的正则表达式实现.
Note that this only works with regex implementations that support "positive lookbehind".
这篇关于正则表达式忽略问号符号后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!