Java如何根据输入检查多个正则表达式模式?

Java如何根据输入检查多个正则表达式模式?,java,regex,Java,Regex,(如果我选择了完全错误的方向,请告诉我是否有更好的方法) 我有一个Java程序,它将有多个模式,我想与一个输入进行比较。如果其中一个模式匹配,那么我希望将该值保存在字符串中。我可以让它与一个单一的模式,但我想能够检查对许多 现在我要检查一个输入是否匹配一个模式: Pattern pattern = Pattern.compile("TST\\w{1,}"); Matcher match = pattern.matcher(input); String ID = match.find()?matc

(如果我选择了完全错误的方向,请告诉我是否有更好的方法)

我有一个Java程序,它将有多个模式,我想与一个输入进行比较。如果其中一个模式匹配,那么我希望将该值保存在字符串中。我可以让它与一个单一的模式,但我想能够检查对许多

现在我要检查一个输入是否匹配一个模式:

Pattern pattern = Pattern.compile("TST\\w{1,}");
Matcher match = pattern.matcher(input);
String ID = match.find()?match.group():null;
因此,如果输入是TST1234或abcTST1234,则ID=“TST1234”

我希望有多种模式,如:

Pattern pattern = Pattern.compile("TST\\w{1,}");
Pattern pattern = Pattern.compile("TWT\\w{1,}");
...
然后访问一个集合,然后根据输入检查每个集合:

List<Pattern> rxs = new ArrayList<Pattern>();
rxs.add(pattern);
rxs.add(pattern2);

String ID = null;

for (Pattern rx : rxs) {
    if (rx.matcher(requestEnt).matches()){
        ID = //???
    }
}

但我不知道该怎么做,也不知道该怎么做。欢迎提供任何帮助或建议。谢谢


编辑:是,模式将随时间而改变。因此,模式列表将不断增长


我只需要得到匹配的字符串…即如果输入是abcTWT123,它将首先检查“TST\w{1,}”,然后移动到“TWT\w{1,}”,因为它匹配ID字符串将设置为“TWT123”

也许您只需要在第一个模式匹配时结束循环:

// TST\\w{1,}
// TWT\\w{1,}
private List<Pattern> patterns;

public String findIdOrNull(String input) {
  for (Pattern p : patterns) {
    Matcher m = p.matcher(input);
    // First match. If the whole string must match use .matches()
    if (m.find()) {
      return m.group(0);
    }
  }
  return null; // Or throw an Exception if this should never happen
}
//TST\\w{1,}
//行波管\\w{1,}
私有列表模式;
公共字符串findIdOrNull(字符串输入){
对于(模式p:模式){
匹配器m=p.Matcher(输入);
//第一个匹配项。如果整个字符串必须匹配,请使用.matches()
if(m.find()){
返回m.group(0);
}
}
返回null;//如果永远不会发生这种情况,则引发异常
}

要在结果中收集匹配的字符串,如果匹配的字符串少于整个字符串,则可能需要在regexp中创建一个组:

List<Pattern> patterns = new ArrayList<>();
patterns.add(Pattern.compile("(TST\\w+)");
...

Optional<String> result = Optional.empty();
for (Pattern pattern: patterns) {
    Matcher matcher = pattern.match();
    if (matcher.matches()) {
        result = Optional.of(matcher.group(1));
        break;
    }
}
然后,您可以在单个操作中进行匹配和分组。然而,随着时间的推移,改变比赛可能会更加困难


组1是第一个匹配的组(即第一组括号内的匹配)。第0组是全部匹配项。因此,如果您想要整个匹配(我不确定您的问题),那么您可能可以使用组0。

使用替代的
|
(正则表达式或):

然后只检查一次图案


还请注意,
{1,}
可以替换为
+

如果您的模式都是简单的前缀,如示例TST和TWT,那么您可以一次定义所有这些,并且用户regex alternation
,这样就不需要循环模式

例如:

    String prefixes = "TWT|TST|WHW";
    String regex = "(" + prefixes + ")\\w+";
    Pattern pattern = Pattern.compile(regex);

    String input = "abcTST123";
    Matcher match = pattern.matcher(input);
    String ID = match.find() ? match.group() : null;

    // given this, ID will come out as "TST123"
现在可以从java
.properties
文件或简单的文本文件读入
前缀
;或作为参数传递给执行此操作的方法。
您还可以将前缀定义为逗号分隔的列表或文件中每行一个前缀,然后在传递之前将其转换为
one | two | three | etc


您可能在多个输入上循环,然后只想创建一次
regex
pattern
变量,只为每个单独的输入创建匹配器。

是否要将每个字符串匹配的所有模式保存到映射?我对您想要的最终输出感到有点困惑给定您的示例,它可以像将模式更改为
pattern.compile(“T[SW]T\\w{1,}”)一样简单=>匹配
T
然后
S
-或-
W
然后
T
,或
模式。编译(“(模式a |其他b)\\W{1,}”)。。。一个好的答案取决于有多少个模式,它们有多不同,如何配置或动态。。。在编写代码时,是否所有可能的模式都已知?它们会随着时间而改变吗?是的,模式会随着时间而改变。只有一个模式会匹配(如果有的话),我只需要得到匹配的字符串…也就是说,如果输入是abcTWT123,它将首先检查“TST\\w{1,}”,然后转到“TWT\\w{1,}”,因为匹配的ID字符串将设置为“TWT123”。我最初认为OP也需要一个组,但因为它使用的是
match.find()?match.group()
它不。。。match.find()创建一个隐式组,如果不进行任何中间查找或匹配,match.group()将检索该隐式组。(好吧,这是假设只有一种模式会匹配,并且/或者当你找到匹配的模式时你会停止)是的,这是一个很好的观点。我没有意识到OP就是这个意思。我将编辑我的答案。Pattern::match-method需要CharSequence作为参数。。。
List<Pattern> patterns = new ArrayList<>();
patterns.add(Pattern.compile("(TST\\w+)");
...

Optional<String> result = Optional.empty();
for (Pattern pattern: patterns) {
    Matcher matcher = pattern.match();
    if (matcher.matches()) {
        result = Optional.of(matcher.group(1));
        break;
    }
}
Optional<String> result = patterns.stream()
    .map(Pattern::match).filter(Matcher::matches)
    .map(m -> m.group(1)).findFirst();
Pattern pattern = Pattern.compile("(TST\\w+|TWT\\w+|...");
Pattern pattern = Pattern.compile("TST\\w+|TWT\\w+|etc");
    String prefixes = "TWT|TST|WHW";
    String regex = "(" + prefixes + ")\\w+";
    Pattern pattern = Pattern.compile(regex);

    String input = "abcTST123";
    Matcher match = pattern.matcher(input);
    String ID = match.find() ? match.group() : null;

    // given this, ID will come out as "TST123"