1

I want to split the string using space and single quote. But two single quotes should be ignored.

Input:

name eq 'a''b'

Output should be:

name

eq

a''b

I tried to use

[^\\s\"(?<!')'(?!')]+|\"[^\"]*\"|(?<!')'(?!')[^(?<!')'(?!')]*(?<!')'(?!')"

but it does not work.

Madhura Adawadkar
  • 192
  • 1
  • 1
  • 11
  • You should be able to adapt the answer I've provided here: http://stackoverflow.com/questions/7804335/split-string-on-spaces-except-if-between-quotes-i-e-treat-hello-world-as – aioobe Aug 18 '14 at 09:37
  • 1
    Can there be spaces in the `'....'` strings? – aioobe Aug 18 '14 at 09:39
  • Can you provide more context on your input string? Perhaps a more specific method is needed... – Adrian Aug 18 '14 at 09:39

5 Answers5

3

The following one should suit your needs:

'(?:[^']|'')+'|[^ ]+

Regular expression visualization

Debuggex Demo


Java example:

String input = "foobar foo bar 'foobar' 'foo bar' 'foo''bar'";
Pattern pattern = Pattern.compile("'(?:[^']|'')+'|[^ ]+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    String match = matcher.group();
    System.out.println(match);
}

Ideone Demo

sp00m
  • 44,266
  • 23
  • 127
  • 230
1

Hi the actual regex is

"\\s|(?<!')'(?!')"

It produces output array as follows

[name, eq, , a''b]
Mohan Raj B
  • 995
  • 6
  • 13
0

This can be done without regex too.

String s = "name eq 'a''b'";
String[] split = new String[3];

Scanner in = new Scanner(s);

split[0] = in.next();
split[1] = in.next();

String temp = in.next();
split[2] = temp.substring(1,temp.length() - 1);

//test output
for(String x : split)
    System.out.println(x);

This will print:

name
eq
a''b

If you need a more generic solution you need to provide more details.
Hope this helps.

qbit
  • 2,528
  • 2
  • 12
  • 27
  • Your code is not optimal for all of cases. How about the input string which you're don't know in advance about? Eg: `lskh asd 'nkhj'g''hgfbgiuy' kjasfg'hb 'jhgb 'hjjh' gb'` – SeniorJD Aug 18 '14 at 09:46
  • Which is why i am asking for more details. I guess you will agree that its pretty unclear if his input follows the same pattern or if there are spaces allowed in between `' '` Or what to do if there is a `'` in between two chars. Which is why i asked for more details. But my answer covers the concept of split in `" "` first and stripping `'` after. Don't you think? – qbit Aug 18 '14 at 09:51
  • At my work, I need to write my code clear and useful for all of cases. Suppose, the most part of programmers in the world (except India ;) ), follows that rule. I think that your way is not useful for all of cases, and that is why downvoted. Sorry :) Wish you a clean code :) – SeniorJD Aug 18 '14 at 09:58
  • Yes. If i knew all cases i would. – qbit Aug 18 '14 at 10:04
0

Here is how I would do:

String value = "name eq 'a''b'";
value = value.replaceAll(" '", " ").replaceAll("'$", "");
System.out.println(value);
String[] arr = value.split(" ");
user1401472
  • 1,760
  • 3
  • 15
  • 31
0

You can try with Lookaround

Pattern

(?<!')'(?!')

Sample code:

System.out.println(Arrays.toString("name eq 'a''b'".split("(?<!')'(?!')")));

output:

[name eq , a''b] 

Pattern explanation:

  (?<!                     look behind to see if there is not:
    '                        '\''
  )                        end of look-behind
  '                        '\''
  (?!                      look ahead to see if there is not:
    '                        '\''
  )                        end of look-ahead

To split based on space or single quote use

 *(?<!')[ '](?!') *

sample code:

System.out.println(Arrays.toString("name eq 'a''b'".split(" *(?<!')[ '](?!') *"))); 

output:

[name, eq, a''b]
Braj
  • 44,339
  • 5
  • 51
  • 69
  • @sp00m to make sure just test it. It returns `[name eq , a, 'b] ` – Braj Aug 18 '14 at 09:57
  • 1
    IMHO you've got the best answer, except you must split on spaces and/or the single quote, not just single quote. Ie try this regex `" *(? – Bohemian Aug 18 '14 at 09:58
  • @Bohemian yes added with few changes. – Braj Aug 18 '14 at 10:05
  • @user3218114 You'll split arguments like ``'foos'' bar'` at the interior space as well (not sure if OP wants that behaviour but I guess no). – AlexR Aug 18 '14 at 10:44
  • @AlexR yes I agree there are lots of possibilities. Need clarification from OP first. – Braj Aug 18 '14 at 10:49