Java: Understanding the String replaceAll() method

前端 未结 2 685
后悔当初
后悔当初 2021-01-02 21:48

I\'m looking to figure out the answer to this problem here.

First off,

blah[abc] = blah[abc].replaceAll(\"(.*) (.*)\", \"$2, $1\");

相关标签:
2条回答
  • 2021-01-02 22:18

    Your regular expression "(.)(.)" will be of this sort : "(x)(y)" this will be replaced by "$2,$1.

    0 讨论(0)
  • 2021-01-02 22:22

    (.*) - would be a pattern to match any number of characters. Parentheses would be to mark it as a sub pattern (for back reference).

    $2 & $1 - are back references. These would be things matched in your second and first sub pattern.

    Basically replaceAll("(.) (.)", "$2, $1") would find characters separated by a space, then add a comma before the space, in addition to flipping the parts. For example:

    a b => b, a
    Hello world => Hellw, oorld
    

    Not sure about nesting... Can you post the code you're running?

    0 讨论(0)
提交回复
热议问题