簡體   English   中英

如何在另一個模式中再次使用聲明的正則表達式模式?

[英]How do I use declared regex Pattern again in another Pattern?

我需要匹配嵌套數組的模式......

我有一個Pattern來處理一個帶數字的數組。 我想知道如何將單數組Pattern用於嵌套數組。

這是我想說的......

[1 2 3 -34]我有這個......

Pattern digit = Pattern.compile("^[(((-?[1-9][0-9]*)\\s*)+)");

[1 2 [-34 7] 34]我需要一個使用先前定義的模式來處理這個問題的模式。

如何回收[1 2 [-34 7] 34]樣品的數字模式?

您需要編寫自己的解析器( src代碼 )。 我寫過類似的東西。 應用程序僅說如果它是格式良好的陣列

public class Main {

    private enum Type {
      LB, RB, NUMBER, END
    }

    private static class Token {
      Type type;
      String value;

      public Token(Type type, String value) {
          super();
          this.type = type;
          this.value = value;
      }

      @Override
      public String toString() {
          return "Token [type=" + type + ", value=" + value + "]";
      }

    }

    private static class Lexer {

      private int current;
      private String input;

      public Lexer(String input) {
          this.input = input;
      }

      private char getChar() {
          return input.charAt(current++);
      }

      private void unputChar() {
          current--;
      }

      private boolean hasNextChar() {
          return current < input.length();
      }

      Token next() {

          if (!hasNextChar()) {
            return new Token(Type.END, "");
          }

          char c = getChar();

          while (Character.isWhitespace(c)) {
            c = getChar();
          }

          if (c == '[') {
            return new Token(Type.LB, "[");
          }

          if (c == ']') {
            return new Token(Type.RB, "]");
          }

          int s = 1;
          if (c == '-') {
            s = -1;
          } else {
            unputChar();
          }

          StringBuilder buffer = new StringBuilder();
          while (hasNextChar()) {

            c = getChar();

            if (Character.isDigit(c)) {
              buffer.append(c);
            } else {
              unputChar();
              break;
            }

          }

          return new Token(Type.NUMBER, s > 0 ? buffer.toString() : "-" + buffer.toString());

      }
    }

    private static boolean match(Type type) {
      return type == currentToken.type;
    }

    private static void consume(Type type) {
      if (!match(type)) {
          throw new RuntimeException(String.format("Should be %s is %s", type.name(), currentToken.type.name()));
      }
      currentToken = lexer.next();
    }

    private static void array() {

      consume(Type.LB);

      while (true) {

          if (match(Type.NUMBER)) {
            consume(Type.NUMBER);
          } else if (match(Type.LB)) {
            array();
          } else {
            break;
          }

      }

      consume(Type.RB);
    }

    private static Lexer lexer;
    private static Token currentToken;

    private static void parse(String line) {

      lexer = new Lexer(line);
      currentToken = lexer.next();

      try {
          array();
          consume(Type.END);
          System.out.println(String.format("%s is a proper array", line));
      } catch (Exception e) {
          System.out.println(String.format("%s is not a proper array because %s", line, e.getMessage()));
      }

    }

    public static void main(String[] args) {

      parse("[1 2 [-34 7] 34]");
      parse("[1 2 -34 7] 34]");
      parse("[1 2 [-34] [7] 34]");
      parse("[1 2 [-34 [7] 34]");
      parse("[1 2 [-34 [7] 34]]");

    }

}

日期:

[1 2 [-34 7] 34] is a proper array
[1 2 -34 7] 34] is not a proper array because Should be END is NUMBER
[1 2 [-34] [7] 34] is a proper array
[1 2 [-34 [7] 34] is not a proper array because Should be RB is END
[1 2 [-34 [7] 34]] is a proper array

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM