正则表达式忽略问号符号后的所有内容 [英] Regexp that ignores everything after a question mark symbol

查看:701
本文介绍了正则表达式忽略问号符号后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要使用正则表达式解析的完整字符串:

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屋!

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