I need to match pattern for nested arrays...
I have a Pattern
that takes care of one array with digits. I was wondering how can I use the single array Pattern
for nested array.
Here is what I'm trying to say...
[1 2 3 -34] I have this covered...
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.
How can I recycle digit Pattern for [1 2 [-34 7] 34] sample?
You need to write your own parser ( src code ). 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.