java - using regex to split string

Zik

I want this string "Initial: At(Forest), MonsterAt(Chimera,Forest), Alive(Chimera)" to be parsed into: "At(Forest)" , "MonsterAt(Chimera, Forest)" , and "Alive(Chimera)" (I don't need "Initial:").

I used this code from (java - split string using regular expression):

 String[] splitArray = subjectString.split(
        "(?x),   # Verbose regex: Match a comma\n" +
        "(?!     # unless it's followed by...\n" +
        " [^(]*  # any number of characters except (\n" +
        " \\)    # and a )\n" +
        ")       # end of lookahead assertion");

this is the output (the underscore is a space):

Initial: At(Forest)
_MonsterAt(Chimera,Forest)
_Alive(Chimera)

but I don't want to have a space before the string ("_Alive(Chimera)"), and I want to remove the "Initial: " after splitting. If I removed the spaces (except for "Initial") from the original string the output is this:

Initial: At(Forest),MonsterAt(Chimera,Forest),Alive(Chimera)
Bohemian

You can do the whole thing in one line like this:

String[] splitArray = str.replaceAll("^.*?: ", "").split("(?<=\\)), *");

This works by simply splitting on commas following closing brackets, after removing any initial input ending in colon-space.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related