【发布时间】:2016-12-02 02:46:15
【问题描述】:
我目前使用正则表达式的模式
Pattern regex = Pattern.compile("^.*?\/\/([^:\/\s]+)(.*(?=\?|\#))", Pattern.DOTALL);
在处理字符串https://url.spec.whatwg.org/#url-syntax 时,它成功地抓住了我试图避免的/?和#,但是当我尝试https://url.spec.whatwg.org/时出现问题
最后的空格阻止它在第 2 组中找到 /。我尝试在前瞻中包含 \p{Blank},但它什么也没做。
“https://www.google.com/search?q=Regular+Expressions&num=1000”
上面的字符串也一样;它在 ? 之前抓取 /search但是一旦我尝试“https://www.google.com/search”,它就会崩溃。
我该如何解决这个问题?
感谢您的宝贵时间!
【问题讨论】:
-
The whitespace at the end is preventing it from finding只是trim()它。另外,read this SO Answer -
我试过修剪所有的空白,但是在 / 之后仍然找不到没有任何东西的令牌