Anupam Maiti
Anupam Maiti

Reputation: 1610

Split on space ignore if inside quotes using regex / Java code

I have two string like below..I want to split those string on space but ignore space if space found inside quotes....

LA    L'TL0BPC,C'ABC  '   THIS IS COMMENT
LA    C'TL0PC',C'ABC  '   THIS IS COMMENT
MVC   EBW000(4),=C'MPI '  THIS IS ANOTHER' CASE

I want to split those lines like this

LA L'TL0BPC,C'ABC ' THIS IS COMMENT

LA C'TL0PC',C'ABC ' THIS IS COMMENT

How to achieve this using java regex....Any other solution is also acceptable..

I have tried this:

String ODD_QT_REGEX="[ ]+(?=([^'']*'[^'']*')*[^'']*)"; 
String EVEN_QT_REGEX="[ ]+(?=([^'']*'[^'']*')*[^'']*$)"; 

but this doesn't do what I need.

Upvotes: 3

Views: 566

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You could do matching instead of splitting. Splitting according to this "[ ]+(?=([^'']*'[^'']*')*[^'']*)"; regex is possible only if your input has balanced quotes.

Seems like i figured out the problem. Same like the op's regex but this regex won't consider an apostrophe as a single quote. The below regex would match one or more space characters which is followed by

  • \b'\b An apostrophe.
  • | OR
  • '[^']' single quote block.
  • | OR
  • [^'] Any character but not of single quote.
  • (?:\\b'\\b|'[^']*'|[^'])*, zero or more times. Then it must be followed by an end of the line anchor.

Code:

String r = "LA    L'TL0BPC,C'ABC  '  THIS IS COMMENT";
String[] m = r.split("\\s+(?=(?:\\b'\\b|'[^']*'|[^'])*$)");
System.out.println(Arrays.toString(m));

OR

For more exact case, you could replace \b in the above regex with lookarounds.

"\\s+(?=(?:(?<=[a-zA-Z])'(?=[A-Za-z])|'[^']*'|[^'])*$)"

Output:

[LA, L'TL0BPC,C'ABC  ', THIS, IS, COMMENT]

Upvotes: 2

Related Questions