[英]How do I use declared regex Pattern again in another Pattern?
I need to match pattern for nested arrays... 我需要匹配嵌套数组的模式......
I have a Pattern
that takes care of one array with digits. 我有一个
Pattern
来处理一个带数字的数组。 I was wondering how can I use the single array Pattern
for nested array. 我想知道如何将单数组
Pattern
用于嵌套数组。
Here is what I'm trying to say... 这是我想说的......
[1 2 3 -34] I have this covered... [1 2 3 -34]我有这个......
Pattern digit = Pattern.compile("^[(((-?[1-9][0-9]*)\\s*)+)");
[1 2 [-34 7] 34] I need a pattern that takes care of this using the previously defined pattern. [1 2 [-34 7] 34]我需要一个使用先前定义的模式来处理这个问题的模式。
How can I recycle digit Pattern for [1 2 [-34 7] 34] sample? 如何回收[1 2 [-34 7] 34]样品的数字模式?
You need to write your own parser ( src code ). 您需要编写自己的解析器( src代码 )。 I have written something like that.
我写过类似的东西。 App only says if it is well formed array
应用程序仅说如果它是格式良好的阵列
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]]");
}
}
Out: 日期:
[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.