Java - regex using regex

I need to break the line where there is a comma, but it depends on where the comma is.

As an example

consider the following:

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

I would like the String.split() function to separate them as follows:

 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 this would be expressed in a regular expression.

Symbols / words in brackets are always capital. In other words, there will be no situation where I will have user_is_active(a,b) .

Is there any way to do this?

+7
source share
3 answers

If you have no more than one level of parentheses, you can do a comma separation that is not followed by a close ) before opening ( :

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

Your proposed rule will be translated as

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

but then you skip the split in the text for example

 Org=NASA,Craft=Shuttle 
+12
source

consider using a parser for parsing this kind of query. For example: javacc or antlr

0
source

Alternatively, if you require more than one level of parentheses, you can create a small line parser to parse a character from a string to a character.

0
source

All Articles