Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex (Java): How do I replace "||" with "| |" repeatedly

Tags:

java

regex

If I have a string of pipe-delimited data:

123456|abcd|||65464|hgfhgf

How can I replace any occurrance of || with: | |?

So it would end up looking like this:

123456|abcd| | |65464|hgfhgf

I tried using the simple Java expression:

delimString.replaceAll("\\\|\\\|", "| |");

but that only replaced the first occurrence:

123456|abcd| ||65464|hgfhgf

So I need something to make it repeat (greedily I think).

like image 680
Chris Avatar asked Apr 05 '11 11:04

Chris


People also ask

What is replaceAll \\ s in Java?

Java String replaceAll() The replaceAll() method replaces each substring that matches the regex of the string with the specified text.

How do you find and replace in a regular expression in Java?

The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.

What does \\ mean in Java regex?

Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.


3 Answers

String resultString = subjectString.replaceAll("\\|(?=\\|)", "| ");

The regex explained without Java's double backslashes:

\|   # Match a literal |
(?=  # only if it's followed by
 \|  # another literal |.
)    # End of lookahead assertion

You could even go wild and replace the empty space between two pipes with a space character:

String resultString = subjectString.replaceAll("(?<=\\|)(?=\\|)", " ");
like image 165
Tim Pietzcker Avatar answered Nov 01 '22 20:11

Tim Pietzcker


The problem is here that the match position is already past the 2nd | you replaced, so it does not count. You'll have to use a while loop to do this.

like image 25
Ingo Avatar answered Nov 01 '22 20:11

Ingo


I agree with Ingo - a loop solution is more lines of code but easier to understand (at least it doesn't have to be explained ;) ):

String test = "abc|def||ghi|||jkl";

StringBuilder result = new StringBuilder();
char previous = 0;
for (char c:test.toCharArray()) {
  if (c == '|' && previous == '|')
    result.append(" ");
  result.append(c);
  previous = c;
}

System.out.println(result);
like image 22
Andreas Dolk Avatar answered Nov 01 '22 21:11

Andreas Dolk