1

I must turn String type modified xpathes to real xpathes by Java;

For example;

This kind of xpathes

_HTML_1__BODY_1__FORM_1__INPUT_3_

should turn to

/HTML[1]/BODY[1]/FORM[1]/INPUT[3]

I have no idea, Could you please help me

1

2 Answers 2

5

Like lateralus said, Strings are immutable, so you can't change these.

However, having said that, you can use the replaceAll to return a modified version of the String, for example in this case:

String input = "_HTML_1__BODY_1__FORM_1__INPUT_3_";

String output = input.replaceAll("_(\\d+)_", "[$1]").replaceAll("_", "/");
// output = /HTML[1]/BODY[1]/FORM[1]/INPUT[3]

Edit

As for an explanation of the regex used in this case:

This method uses two separate regular expressions to return a modified String. Firstly, "_(\\d+)_", looks for numbers surrounded by two underscore characters _, \\d is a regex short hand for any digit. The surrounding brackets (...) capture the number, so that we can reference it in the replacement string.

When we make the first replacement, we replace with [$1], in here $1 refers back to the first captured group, i.e. the digit captured, and surrounds it in square brackets [...]. The underscores are also removed, as these were captured in the expression, if not in the group.

The second replaceAll call simply replaces all remaining underscore characters with a /.

Sign up to request clarification or add additional context in comments.

4 Comments

Please, add more explanations on regular expressions for OP.
@DmitryGinzburg I don't see why he should. The OP can research that topic if he chooses to.
@Duncan he may just add something like "for more information, google for regular expression replace" or smth like this.
@DmitryGinzburg explanation added as per your suggestion.
0

Use a state machine:

 public static void main(String []args){
    String inp = "_HTML_1__BODY_1__FORM_1__INPUT_3_";

    String out = "";
    int state = 0;
    for (int i=0; i<inp.length(); i++) {
        if (inp.charAt(i) == '_') {
            switch (state) {
                case 0:
                    out += "/";
                    break;
                case 1:
                    out += "[";
                    break;
                case 2:
                    out += "]";
                    break;
                default:
                    throw new RuntimeException();
            }
            // Next state
            state = (state + 1) % 3;
        } else {
            out += inp.charAt(i);
        }
    }

    System.out.println(out);
    // Returns "/HTML[1]/BODY[1]/FORM[1]/INPUT[3]"
 }

4 Comments

It's better not to use +=, when you're constructing String in many steps, here you should use StringBuffer or StringBuilder, or, easily regular expression replace, as @PeterK suggested.
Of course, I thought OP was mainly interested in an idea how to start with this, not in a millisecond optimized helper function. That said, I would also prefer a regex solution..
Okay, you're using DFA instead of using regular expression? Why should this make sense, if you don't want to make performance the best?
There is no deep reason behind this - it was just the first idea that popped up and before the smarter regexp answer was posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.