2011-05-07 130 views
1

我怎樣才能分割字符串像Java的正則表達式分割

"-3.0*6.7+(5/2)*-0.8--12.98+4^-0.5" 

通過使用正則表達式表達

-3.0,*,6.7,+,(,5,/,2,),*,-0.8,-,-12.98,+,4,^,-0.5 
+1

如何分割'「4-3」'? – kennytm 2011-05-07 13:04:13

+0

@KennyTM 4, - ,3但如果它是「4-3」,那麼4, - , - 3 – 2011-05-07 16:04:00

回答

2

這是不切實際的使用正則表達式完成這個任務:你最好建立某種形式的標記器/詞法分析器從輸入源創建標記。特別是一元減號使得正則表達式難以分割。

但是,爲了回答你的問題,你可能分裂以下模式:

(?=[+*/()^])|(?<=[+*/()^])|(?<=\d-)|(?<=\d)(?=-) 

這意味着:

   # Split on: 
(?=[+*/()^]) # the empty space that has one of: +, *, /, (,),^ahead of it 
|    # OR 
(?<=[+*/()^]) # the empty space that has one of: +, *, /, (,),^before it 
|    # OR 
(?<=\d-)  # the empty space that has a digit followed by a minus sign before it 
|    # OR 
(?<=\d)(?=-) # the empty space that has a digit before it and a minus sign ahead of it 
0

我假設你最終要計算這個表達式。這是一個評估算術表達式的代碼。它通過整數+括號支持基本的算術運算符。使它適應浮點文字應該很容易。

public class CompactSolver { 
    private String input; 

    public CompactSolver(String input_) { 
    input = input_.trim(); 
    } 

    private char peek(int offset) { 
    return offset >= input.length() ? '\0' : 
    input.charAt(offset); 
    } 

    private boolean consumeIf(char c) { 
    if (peek(0) != c) 
    return false; 
    consume(1); 
    return true; 
    } 

    private String consume(int numChars) { 
    if (numChars == 0) 
    throw new RuntimeException("syntax error"); 
    String result = input.substring(0, numChars); 
    input = input.substring(numChars).trim(); 
    return result; 
    } 

    public double eval() { 
    double lhs = mul(); 
    if (consumeIf('+')) 
    return lhs + eval(); 
    else if (consumeIf('-')) 
    return lhs - eval(); 
    return lhs; 
    } 

    private double mul() { 
    double lhs = unary(); 
    if (consumeIf('*')) 
    return lhs * mul(); 
    else if (consumeIf('/')) 
    return lhs/mul(); 
    return lhs; 
    } 

    private double unary() { 
    if (consumeIf('-')) 
    return -unary(); 

    if (consumeIf('(')) { 
    double result = eval(); 
    if (!consumeIf(')')) 
     throw new RuntimeException("Missing ')'"); 
    return result; 
    } 

    return literal(); 
    } 

    private double literal() { 
    for (int i = 0; true; ++i) 
    if (!Character.isDigit(peek(i))) 
     return Integer.parseInt(consume(i)); 
    } 
}