我正在尝试创建一个正则表达式,它与查询字符串前有一个大写字母的URL相匹配。我希望捕获包含问号的查询字符串,并捕获非查询字符串部分。如果没有查询字符串,但有大写字母,则应捕获非查询字符串部分。
下面是几个例子:
/contextroot/page.html?param1=value1¶m2=value2 NO MATCH
/contextroot/page.html?param=VALUE¶m2=value2 NO MATCH
/contextroot/Page.html?param=value MATCH
/contextroot/Page.html GROUP 1
?param=value GROUP 2
/contextroot/page.HTML MATCH
/contextroot/page.HTML GROUP 1这是我在regex中的第一个部分:
^(.*[A-Z].*)(\??.*)$它坏了。这永远不会捕获查询字符串。
发布于 2010-09-11 02:05:28
^/contextroot/([^?]*[A-Z][^?]*)(\?.*)?$解释:
^/contextroot/ # literal start of URL
( # match group 1
[^?]* # anything except `?` (zero or more)
[A-Z] # one capital letter
[^?]* # see above
)
( # match group 2
\? # one ?
.* # anything that follows
)? # optionally
$ # end of string 发布于 2010-09-11 00:49:21
(^/contextroot/(?=[^?A-Z]*[A-Z])[^?]*)(\?.*)?解释:
( # match group 1
^/contextroot/ # literal start of URL (optional, remove if not needed)
(?= # positive look-ahead...
[^?A-Z]* # anything but a question mark or upper-case letters
[A-Z] # a mandatory upper-case letter
) # end look-ahead
[^?]* # match anything but a question mark
) # end group 1
( # match group 2
\?.* # a question mark and the rest of the query string
)? # end group 2, make optional请注意,这是为了检查单个URL,在针对多行字符串运行时不起作用。
要使其适用于多行输入(每行一个URL ),请进行以下更改:
(^/contextroot/(?=[^?A-Z\r\n]*[A-Z])[^?\r\n]*)(\?.*)?https://stackoverflow.com/questions/3686520
复制相似问题