我有以下模式:
(with some words beginning with # or @)+"+sentence that does not contain any question+"
(with some words beginning with # or @) + "+sentence that does not contain any question+"+ with link
"+sentence that does not contain any question+"
例如,这里是一些具有模式的匹配文本:
#cbc @RBC: "This is the video" http://stackoverflow.com/questions/ask
"This is the video" http://stackoverflow.com/questions/ask
#cbc @RBC: "This is the video"
"This is the video"
这是我的正则表达式:
^(\\s[@|#]\\w+){0,}\".+\"((?:http|https):\\/\\/\\S+){0,}$
这是我的代码:
private static void qoutedWrapped(String commentstr){
String urlPattern = "^(\\s[@|#]\\w+){0,}\".+\"((?:http|https):\\/\\/\\S+){0,}$";
Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(commentstr);
if (m.find()) {
System.out.println("yes");
}
}
但是当我尝试使用:
@cbc: "I love u" http://stackoverflow.com/questions/ask
它不起作用
有人可以帮忙吗?
最佳答案
^(\\s[@|#]\w+){0,}\".+\"((?:http|https):\\/\\/\\S+){0,}$
(\\s[@|#]\\w+)
如果相信您的“无效”示例,您希望白色字符成为组中的第一个字符
@cbc:“我为什么爱你?” http://stackoverflow.com/questions/ask
^
和 [@|#]
之间没有必需的白字符,因此不会匹配。我不知道你的正则表达式的其余部分,但正如你所看到的,它需要仔分割析。
尝试使用诸如
https://www.debuggex.com/r/h8w0IB3SMkmEGYri或http://regexpal.com/使用多个测试输入和正则表达式解释/可视化来动态测试您的正则表达式。另外使用 *
而 {0,}
感觉更自然
关于java - 无法匹配具有以下模式的文本 : @some words(optional) :"sometext"+link(optional),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510993/