Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String While Ignoring Escaped Character

Tags:

java

string

regex

I want to split a string along spaces, ignoring spaces if they are contained inside single quotes, and ignoring single quotes if they are escaped (i.e., \' ) I have the following completed from another question.

    String s = "Some message I want to split 'but keeping this a\'s a single string' Voila!";
    for (String a : s.split(" (?=([^\']*\'[^\"]*\')*[^\']*$)")) {
        System.out.println(a);
    }

The output of the above code is

Some
message
I
want
to
split
'but
keeping
this
'a's a single string'
Voila!

However, I need single quotes to be ignored if they are escaped ( \' ), which the above does not do. Also, I need the first and last single quotes and forward slashes removed, if and only if it (the forward slashes) are escaping a single quote (to where 'this is a \'string' would become this is a 'string). I have no idea how to use regex. How would I accomplish this?

like image 962
Tyler Senter Avatar asked Mar 01 '15 03:03

Tyler Senter


People also ask

How do you split a string with escape characters?

split() is based on regex expression, a special attention is needed with some characters which have a special meaning in a regex expression. The special character needs to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" ! See split(String.int).

How do you split a string when there is a space?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a string into characters?

(which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\.") , or use character class [] to represent literal character(s) like so split("[.]") , or use Pattern#quote() to escape the entire string like so split(Pattern.

How do you split a string by a space and a comma?

To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.


2 Answers

You need to use a negative lookbehind to take care of escaped single quotes:

String str = 
        "Some message I want to split 'but keeping this a\\'s a single string' Voila!";

String[] toks = str.split( " +(?=((.*?(?<!\\\\)'){2})*[^']*$)" );
for (String tok: toks)
    System.out.printf("<%s>%n", tok);

output:

<Some>
<message>
<I>
<want>
<to>
<split>
<'but keeping this a\'s a single string'>
<Voila!>

PS: As you noted that escaped single quote needs to be typed as \\' in String assignment otherwise it will be treated as plain '

like image 151
anubhava Avatar answered Oct 07 '22 02:10

anubhava


or you could use this pattern to capture what you want

('(?:[^']|(?!<\\\\)')*'|\S+)  

Demo

like image 38
alpha bravo Avatar answered Oct 07 '22 01:10

alpha bravo