6

I'm kind of stuck trying to come up with regular expression to break up strings with the following properties:

  1. Delimited by the | (pipe) character
  2. If an individual value contains a pipe, escaped with \ (backslash)
  3. If an individual value ends with backslash, escaped with backslash

So for example, here are some strings that I want to break up:

  1. One|Two|Three should yield: ["One", "Two", "Three"]
  2. One\|Two\|Three should yield: ["One|Two|Three"]
  3. One\\|Two\|Three should yield: ["One\", "Two|Three"]

Now how could I split this up with a single regex?

UPDATE: As many of you already suggested, this is not a good application of regex. Also, the regex solution is orders of magnitude slower than just iterating over the characters. I ended up iterating over the characters:

public static List<String> splitValues(String val) {
    final List<String> list = new ArrayList<String>();
    boolean esc = false;
    final StringBuilder sb = new StringBuilder(1024);
    final CharacterIterator it = new StringCharacterIterator(val);
    for(char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if(esc) {
            sb.append(c);
            esc = false;
        } else if(c == '\\') {
            esc = true;
        } else if(c == '|') {
            list.add(sb.toString());
            sb.delete(0, sb.length());
        } else {
            sb.append(c);
        }
    }
    if(sb.length() > 0) {
        list.add(sb.toString());
    }
    return list;
}
3
  • 1
    Let's make it clear. What you want is this: split by | and remove it from the string, don't split by \| and remove \ from the string and finally split by \\| and remove \| from the first part and \ from the second part. How do you think this could be made with one regexp? It seems like completely different situations to me... Commented Jul 29, 2011 at 23:27
  • Is it possible to change your delimiters? Commented Jul 29, 2011 at 23:36
  • I think you guys are right! This might be too much for regex. Commented Jul 29, 2011 at 23:49

1 Answer 1

13

The trick is not to use the split() method. That forces you to use a lookbehind to detect escaped characters, but that fails when the escapes are themselves escaped (as you've discovered). You need to use find() instead, to match the tokens instead of the delimiters:

public static List<String> splitIt(String source)
{
  Pattern p = Pattern.compile("(?:[^|\\\\]|\\\\.)+");
  Matcher m = p.matcher(source);
  List<String> result = new ArrayList<String>();
  while (m.find())
  {
    result.add(m.group().replaceAll("\\\\(.)", "$1"));
  }
  return result;
}

public static void main(String[] args) throws Exception
{
  String[] test = { "One|Two|Three", 
                    "One\\|Two\\|Three", 
                    "One\\\\|Two\\|Three", 
                    "One\\\\\\|Two" };
  for (String s :test)
  {
    System.out.printf("%n%s%n%s%n", s, splitIt(s));
  }
}

output:

One|Two|Three
[One, Two, Three]

One\|Two\|Three
[One|Two|Three]

One\\|Two\|Three
[One\, Two|Three]

One\\\|Two
[One\|Two]
Sign up to request clarification or add additional context in comments.

3 Comments

That's impressive. Could you explain how the pattern works? I still struggle with regular expressions.
This works like a charm!! Thanks again @Alan Moore!! Now how would you do the reverse?
@Paul: The basic idea is that you never match a backslash without consuming the next character, too. That way you'll never get out of sync with the escape sequences. But if you really want to grok regexes, you should read The Book.

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.