我有一个很大的List<String>
其中每个 String 是一个包含 1+ 个“标记”的句子(以“a”或“b”为前缀,后跟一个正整数):
List<String> tokenList = new ArrayList<String>()
tokenList.add("How now a1 cow.")
tokenList.add("The b1 has oddly-shaped a2.")
tokenList.add("I like a2! b2, b2, b2!")
// etc.
我想编写一个接受可变参数标记列表的函数,并将返回 tokenList
的一个子集包含所有 标记参数的字符串。例如:
public class TokenMatcher {
List<String> tokenList; // Same tokenList as above
List<String> findSentencesWith(String... tokens) {
List<String> results = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
// Build up the regex... (TODO: this is where I'm going wrong)
for(String t : tokens) {
sb.append(t);
sb.append("|");
}
String regex = sb.toString();
for(String sentence : tokenList) {
if(sentence.matches(regex)) {
results.add(sentence);
}
}
return results;
}
}
同样,正则表达式的构建方式必须所有 tokens
传递给函数的参数必须存在于句子内部才能使匹配为真。因此:
TokenMatcher matcher = new TokenMatcher(tokenList);
List<String> results = matcher.findSentencesWith("a1"); // Returns 1 String ("How now a1 cow")
List<String> results2 = matcher.findSentencesWith("b1"); // Returns 1 String ("The b1 has oddly-shaped a2.")
List<String> results3 = matcher.findSentencesWith("a2"); // Returns the 2 Strings with a2 in them since "a2" is all we care about...
List<String> results4 = matcher.findSentencesWith("a2", "b2"); // Returns 1 String ("I like a2! b2, b2, b2!.") because we care about BOTH tokens
最后一个示例 ( results4
) 很重要,因为尽管“a2”标记出现在几个句子中,但 results4
我们要求该方法为我们提供包含 both 标记的句子的匹配项。这是 n 元合取,意味着如果我们指定 50 个标记作为参数,我们只需要包含所有 50 个标记的句子。
以上findSentencesWith
example 是我迄今为止最好的尝试。有什么想法吗?
最佳答案
鉴于您声明的要求,即顺序和频率都不重要,我认为在这种情况下根本不需要使用正则表达式。
相反,您可以将每个字符串与提供的所有示例标记进行比较,看看字符串中是否包含所有内容。如果是这样,它在结果集中。第一次检测到丢失的标记时,将从结果集中删除该字符串。
这种代码看起来像这样:
TokenMatcher.java
package so_token;
import java.util.*;
public class TokenMatcher {
public TokenMatcher(List<String> tokenList) {
this.tokenList = tokenList;
}
List<String> tokenList;
List<String> findSentencesWith(String... tokens) {
List<String> results = new ArrayList<String>();
// start by assuming they're all good...
results.addAll(tokenList);
for (String str : tokenList) {
for(String t : tokens) {
// ... and remove it from the result set if we fail to find a token
if (!str.contains(t)) {
results.remove(str);
// no point in continuing for this token
break;
}
}
}
return results;
}
public static void main (String[] args) throws java.lang.Exception
{
List<String> tokenList = new ArrayList<String>();
tokenList.add("How now a1 cow.");
tokenList.add("The b1 has oddly-shaped a2.");
tokenList.add("I like a2! b2, b2, b2!");
TokenMatcher matcher = new TokenMatcher(tokenList);
List<String> results = matcher.findSentencesWith("a1"); // Returns 1 String ("How now a1 cow")
for (String r : results) {
System.out.println("1 - result: " + r);
}
List<String> results2 = matcher.findSentencesWith("b1"); // Returns 1 String ("The b1 has oddly-shaped a2.")
for (String r : results2) {
System.out.println("2 - result: " + r);
}
List<String> results3 = matcher.findSentencesWith("a2"); // Returns the 2 Strings with a2 in them since "a2" is all we care about...
for (String r : results3) {
System.out.println("3 - result: " + r);
}
List<String> results4 = matcher.findSentencesWith("a2", "b2"); // Returns 1 String ("I like a2! b2, b2, b2!.") because we care about BOTH tokens
for (String r : results4) {
System.out.println("4 - result: " + r);
}
}
}
这会产生以下输出:
1 - result: How now a1 cow.
2 - result: The b1 has oddly-shaped a2.
3 - result: The b1 has oddly-shaped a2.
3 - result: I like a2! b2, b2, b2!
4 - result: I like a2! b2, b2, b2!
在 ideone 上稍作调整,可运行的代码(主要围绕无包名称和非公共(public)类,因此它将在网站上运行) .
注意:根据您提供的信息,并且由于函数正在接受标记列表,看来 contains
足以确定 token 存在。但是,如果事实证明对此有额外的限制,例如 token 必须后跟一个空格或一组标点符号中的一个,或类似的东西,才能算作一个 token ,那么我 会建议使用正则表达式——基于单个标记——将contains
替换为matches
并传入正则表达式 定义你想要围绕 token 的内容。
可能还需要一个函数来验证传递给 findSentencesWith
函数的 tokenList
。
关于java - Java 中的连接正则表达式匹配标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25634227/