【发布时间】:2014-08-20 19:03:39
【问题描述】:
我想检查一个字符串是否包含以特定顺序直接跟随的两个单词/字符串。 标点符号也应包含在单词/字符串中。 (即 "word" 和 "word." 应作为不同的词处理)。
举个例子:
String word1 = "is";
String word1 = "a";
String text = "This is a sample";
Pattern p = Pattern.compile(someregex+"+word1+"someregex"+word2+"someregex");
System.out.println(p.matcher(text).matches());
这应该打印出 true。
使用以下变量,它也应该打印 true。
String word1 = "sample.";
String word1 = "0END";
String text = "This is a sample. 0END0";
但后者在设置 word1 = "sample" 时应返回 false(不带标点符号)。
有谁知道正则表达式字符串应该是什么样子(即我应该写什么而不是“someregex”?)
谢谢!
【问题讨论】:
标签: java regex word punctuation