how to break string that has format like "java method signature" into part?

java regex string parsing split

70 观看

1回复

23 作者的声誉

I have a complex strings like the following format pattern:

(Command)(Parameter_1,Parameter_2,Parameter_3,Parameter_n)

Example: PADDING_LEFT(LAST_USE_COUNTER(1450,5),5,0)

I want to split them as following:

Command:PADDING_LEFT

Parameter_1:LAST_USE_COUNTER(1450,5)

Parameter_2: 5

Parameter_3: 0

Is there a way to a break such string?

作者: Tuong Luong 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

198878 作者的声誉

决定

I would go down the regex route. A more robust approach could be to use a Java syntax parser, such as JavaParser:

MethodCallExpr bs =
    (MethodCallExpr) JavaParser.parseExpression
                    ("PADDING_LEFT(LAST_USE_COUNTER(1450,5),5,0)");
System.out.println(bs.getName());
List<Expression> arguments = bs.getArguments();
for (int i = 0; i < arguments.size(); ++i) {
    System.out.printf("Argument_%d: %s%n", (i + 1), arguments.get(i));
}
作者: Mureinik 发布者: 2017 年 12 月 27 日
32x32