[英]How can I add an underscore before each capital letter inside a Java String?
[英]Which Java Regex can I use to match a URL that has a capital letter before the query string?
我正在尝试创建一个与查询字符串前面有大写字母的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
这是我在正则表达式上的第一次剪辑:
^(.*[A-Z].*)(\??.*)$
它被破坏了。 这从不捕获查询字符串。
(^/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]*)(\?.*)?
^/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.