Reputation: 7143
I have string in the following form:
HOME(SPADE0) HOME(HEART0) HOME(CLUB0) BOTTOMCOL(CLUBA) ON(HEART2 CLUBA)
I would lilke to split it into
HOME(SPADE0)
HOME(HEART0)
HOME(CLUB0)
BOTTOMCOL(CLUBA)
ON(HEART2 CLUBA)
splitting at space splits the last token also, which I don't want . What can be a suitable regular expression for it?
Thanks in advance!
EDIT
String[] tokens = line.split("[)]\\s+");
Upvotes: 1
Views: 137
Reputation: 116
Why not just split on the ")" and then append it to all found tokens?
String [] results = str.split( ")" );
String token1 = results[0].trim() + ")"; // the trim is to remove leading spaces
This is assuming that all your data matches the presented format.
Upvotes: 0
Reputation: 213391
Try this regex (Using Negative look-ahead
): -
String[] arr = str.split("\\s+(?![^(]*\\))");
System.out.println(Arrays.toString(arr));
It will only split on space, which is not in between (
and )
.
OUTPUT: -
[HOME(SPADE0), HOME(HEART0), HOME(CLUB0), BOTTOMCOL(CLUBA), ON(HEART2 CLUBA)]
Explanation: -
\\s+ // split on space (one or more)
(?! // Negative look ahead (Not followed by)
[^(]* // Anything except `(` (0 or more)
\\) // Ending with `)`
) // End
So, if your space is between, (
and )
as in (HEllo World)
.
It will not match the above regex. Because the space in there is followed by : -
[^(]* // Any string not containing `(` - World
\\) // Ending with `)`
Note that, although this will solve your problem with split
. But ideally, this should be done with Pattern
and Matcher
. As in @Marko's answer.
Upvotes: 3
Reputation: 31358
This should work:
Pattern ptrn = Pattern.compile("\\w+\\(.+?\\)");
Upvotes: 1
Reputation: 200296
Better split by matching the content instead of the delimiters:
final Matcher m = Pattern.compile("\\w+\\(.*?\\)").matcher(input);
final List<String> matches = new ArrayList<>();
while (m.find()) matches.add(m.group());
Upvotes: 7