Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - split string using regular expression

I need to split a string where there's a comma, but it depends where the comma is placed.

As an example

consider the following:

C=75,user_is_active(A,B),user_is_using_app(A,B),D=78

I'd like the String.split() function to separate them like this:

C=75 

user_is_active(A,B) 

user_using_app(A,B)

D=78

I can only think of one thing but I'm not sure how it'd be expressed in regex.

The characters/words within the brackets are always capital. In other words, there won't be a situation where I will have user_is_active(a,b).

Is there's a way to do this?

like image 567
kkudi Avatar asked Mar 19 '11 16:03

kkudi


People also ask

How do you split a string in regex?

To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.

How do you split a string in Java?

Split() String method in Java with examples. The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

What does split \\ s+ do in Java?

split("\\s+") will split the string into string of array with separator as space or multiple spaces. \s+ is a regular expression for one or more spaces.

What method in Java Lang string would you use to split the string?

As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.


1 Answers

If you don't have more than one level of parentheses, you could do a split on a comma that isn't followed by a closing ) before an opening (:

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

Your proposed rule would translate as

String[] splitArray = subjectString.split(
    "(?x),        # Verbose regex: Match a comma\n" +
    "(?<!\\p{Lu}) # unless it's preceded by an uppercase letter\n" +
    "(?!\\p{Lu})  # or followed by an uppercase letter");

but then you would miss a split in a text like

Org=NASA,Craft=Shuttle
like image 103
Tim Pietzcker Avatar answered Sep 20 '22 05:09

Tim Pietzcker