java - 无法匹配具有以下模式的文本 : @some words(optional) :"sometext"+link(optional)

标签 java regex

我有以下模式:

(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,}$

Regular expression visualization

(\\s[@|#]\\w+) 如果相信您的“无效”示例,您希望白色字符成为组中的第一个字符

@cbc:“我为什么爱你?” http://stackoverflow.com/questions/ask ^[@|#] 之间没有必需的白字符,因此不会匹配。我不知道你的正则表达式的其余部分,但正如你所看到的,它需要仔分割析。

尝试使用诸如 https://www.debuggex.com/r/h8w0IB3SMkmEGYrihttp://regexpal.com/使用多个测试输入和正则表达式解释/可视化来动态测试您的正则表达式。另外使用 *{0,} 感觉更自然

关于java - 无法匹配具有以下模式的文本 : @some words(optional) :"sometext"+link(optional),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510993/

相关文章:

java - 将 pcm 数据转换为低尺寸音频类型 - Java/Android

python - 正则表达式子电话号码格式在同一字符串上多次

swift - 货币正则表达式 - 从计数限制中排除逗号

java - 将 DukeScript 客户端工件打包到 war 中并从 Web 容器(例如 Tomcat)提供服务

java - 生成某些数字范围的所有排列的序列第二部分

java - (如何)外包 GWT 国际化?

javascript - 正则表达式匹配 URL 末尾的文件名

java - 如何通过ajax调用servlet加载一个值,servlet返回什么到html tabel rowcell

Javascript 正则表达式-如何在组内查找 PascalCase 项目

Java - 在字符串模式后将字符串插入另一个字符串?