0

I have the following string

String path = "(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)";

I used following code,

String delims = "\\.";

String[] tokens = path.split(delims);
int tokenCount = tokens.length;
for (int j = 0; j < tokenCount; j++) {
    System.out.println("Split Output: "+ tokens[j]);
}

Current output is

Split Output: (tags:homepage)
Split Output: (tags:project mindshare)
Split Output: (tags:5
Split Output: 5
Split Output: x)
Split Output: (tags:project 5
Split Output: x)

End goal is to convert this string into

String newpath = "(tags:homepage)(tags:project mindshare)(tags:5.5.x)(tags:project 5.x)";
7
  • 2
    Why not use replaceAll(").(",")("? Commented Aug 8, 2016 at 9:18
  • 1
    Then use 'System.out.print' and not 'println' if you don't want newlines. Commented Aug 8, 2016 at 9:18
  • @MuratK. - Its not that simple. He will need to replace only those . which are preceeded by a ( Commented Aug 8, 2016 at 9:19
  • 2
    Possible duplicate of Regex to match only commas not in parentheses? Commented Aug 8, 2016 at 9:19
  • 1
    @TheLostMind Yeah, I edited it and provided a replaceAll answer. Commented Aug 8, 2016 at 9:24

3 Answers 3

3

You can use a positive lookbehind to achieve this :

String path = "(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)";
String newPath = path.replaceAll("(?<=\\))\\.", ""); // look for periods preceeded by `)`
System.out.println(newPath);

O/P :

(tags:homepage)(tags:project mindshare)(tags:5.5.x)(tags:project 5.x)
Sign up to request clarification or add additional context in comments.

4 Comments

Any other better way of doing it, other then replaceAll ?
@srinisunka - Yes, there are, split and append for example.. Or even indexOf + subString etc in a loop. But none as elegant as replaceAll
Then use split() using the same regex. Then you will get individual tokens which can then be changed @srinisunka
@srinisunka - If you want a solution without using regex, then check Tunaki's answer.
3

You can do this without using a regular expression, in a clean and simple way, that even supports nested parentheses (for the day you'll need to support them).

What you want to do here is to keep everything that is inside parentheses, so we can just loop over the characters and keep a count of open parentheses. If this count is greater than 0, we add the character (it means we're inside parens); if not, we disregard the character.

String path = "(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)";

StringBuilder sb = new StringBuilder();
int openParens = 0;
for (char c : path.toCharArray()) {
    if (c == '(') openParens++;
    if (openParens > 0) sb.append(c);
    if (c == ')') openParens--;
}

System.out.println(sb.toString());

This has the advantage that you don't implicly rely on: having a single dot between parens, not having nested parens, etc.

Comments

0

Here is a simple approach.

System.out.println("(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)".replaceAll("\\)\\.\\(", ")("));

output is

(tags:homepage)(tags:project mindshare)(tags:5.5.x)(tags:project 5.x)

Comments

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.