[英]How to I replace brackets (), keep the contents and tab the result in Java?
我想在制表符和添加換行符的同時替換字符串中的括號,例如打印精美的文字。
(foo) AND ((bar) OR (baz))
變
(
foo
)
AND
(
(
bar
)
OR
(
baz
)
)
我努力了:
"((foo) OR ((bar)(baz)))".replaceAll("\\((.*?)\\)", "\\(\n\t$1\n\\)")
但效果不佳。
laune azurefrog有權利。 您提供的表達式不是正則表達式,因此您不能使用正則表達式引擎來修飾表達式。 在我看來,您最好的辦法是編寫一個可以處理此任務的解析器。 我一直試圖將解析器邏輯與處理業務的代碼分開。 您會注意到,在ExpressionListener
的實現中處理席位格式化的邏輯。 在下面可以找到代碼。
public class PrettyPrintExample {
private interface ExpressionListener {
void lb();
void rb();
void content(String content);
}
private enum Type {
LB, RB, STRING, 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, ")");
}
unputChar();
StringBuilder buffer = new StringBuilder();
while (hasNextChar()) {
c = getChar();
if (c != '(' && c != ')' && !Character.isWhitespace(c)) {
buffer.append(c);
} else {
unputChar();
break;
}
}
return new Token(Type.STRING, buffer.toString());
}
}
private static Lexer lexer;
private static Token currentToken;
public static void parse(String line, ExpressionListener listener) {
lexer = new Lexer(line);
currentToken = lexer.next();
expression(listener);
consume(Type.END);
}
private static void expression(ExpressionListener listener) {
while (true) {
if (match(Type.STRING)) {
listener.content(currentToken.value);
consume(Type.STRING);
} else if (match(Type.LB)) {
consume(Type.LB);
listener.lb();
expression(listener);
consume(Type.RB);
listener.rb();
} else {
break;
}
}
}
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();
}
public static void main(String[] args) {
String line = "(foo) AND ((bar) OR (baz))";
parse(line, new ExpressionListener() {
private int indent = 0;
private void indent(int indent) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < indent; i++) {
builder.append("\t");
}
System.out.print(builder.toString());
}
private void nl() {
System.out.println();
}
@Override
public void lb() {
indent(++indent);
System.out.print("(");
nl();
}
@Override
public void rb() {
indent(indent);
System.out.print(")");
indent--;
nl();
}
@Override
public void content(String content) {
indent(indent);
System.out.print(content);
nl();
}
});
}
}
產量
(
foo
)
AND
(
(
bar
)
OR
(
baz
)
)
我知道對您的案例使用CFL解析器不是最簡單的解決方案,但是請注意,如果您的表達式變得非常復雜,則擴展解析器確實很容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.