简体   繁体   中英

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?

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

consider using a parser generator for parsing this kind of query. Eg: javacc or antlr

作为替代方案,如果您需要多个括号级别,则可以创建一个小字符串解析器来逐字符解析字符串。

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