I'm looking for the correct regex to provide me the following results:
- it needs to group words surrounded by single/double quote
- it needs to keep printing the single quote when there's no other single quote in the string
- when not surrounded by single/double quotes - split on space
I currently have:
Pattern pattern = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
... but the following examples are not completely working. Who can help me with this one?
Examples:
- foo bar
- group1: foo
- group2: bar
- description: split on space
- "foo bar"
- group1: foo bar
- description: surrounded by double quotes so group foo and bar, but don't print double quotes
- 'foo bar'
- group1: foo bar
- description: same as above, but with single quotes
- 'foo bar
- group1: 'foo
- group2: bar
- description: split on space and keep single quote
- "'foo bar"
- group1: 'foo bar
- description: surrounded by double quotes so group 'foo and bar and keep single quote
- foo bar'
- group1: foo
- group2: bar'
- foo bar"
- group1: foo
- group2: bar"
- "foo bar" "stack overflow"
- group1: foo bar
- group2: stack overflow
- "foo' bar" "stack overflow" how do you do
- group1: foo' bar
- group2: stack overflow
- group3: how
- group4: do
- group5: you
- group6: do