简体   繁体   中英

regular expression for string split in java

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+"); 

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());

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.

这应该工作:

Pattern ptrn = Pattern.compile("\\w+\\(.+?\\)");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM