2

我需要定义多模式以使用 String 进行编译,并且在运行后它应该给我在我的模式中具有相同格式的字符串中的任何内容。这是代码:

     String line = "This order was places for QT 30.00$ !OK ? ";

  Pattern[] patterns = new Pattern[]{
        Pattern.compile("\\d+[.,]\\d+.[$] ", Pattern.CASE_INSENSITIVE),
        Pattern.compile("\\d:\\d\\d",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)     
        };      // Create a Pattern object

  // Now create matcher object.
      for (Pattern scriptPattern : patterns){
          Matcher m = scriptPattern.matcher(line);
 System.out.println(m.group());
         }      }
4

2 回答 2

0

这是你想要的

private static Pattern[] patterns = new Pattern[]{
    Pattern.compile("Your pattern ", Pattern.CASE_INSENSITIVE),
    Pattern.compile("your pattern",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)     
    };

您可以使用它来浏览模式并匹配它们

 for (Pattern scriptPattern : patterns){
                    Matcher m = scriptPattern.matcher(line)
                     while (m.find()) {
                 String d = m.group();
                 if(d != null) {
                     System.out.print(d);

                 }

            }
    }
于 2013-05-04T06:26:04.490 回答
0

在编辑之前使用您的原始问题进行一些调整以使其正常工作:

public static void main(String[] args) {
    // String to be scanned to find the pattern.
    String line = "This order was places for QT 30.00$ !OK ? 2:37 ";

    String pattern = "(\\d+[.,]\\d+.[$])(.*)(\\d:\\d\\d)";
    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find()) {
        System.out.println("Found value: " + m.group(1) + " with time: " + m.group(3));
    }  
}

输出:

Found value: 30.00$ with time: 2:37
于 2013-05-04T07:06:05.943 回答