简体   繁体   中英

Java regex to find a method's arguments

I'm trying to find a regex to split a call to a method

boo(a,b), c

so it would return

boo(a,b)

and

c

after splitting by it. Is it possible doing it with regex? Thanks.

Regular languages cannot express recursion. At the point where you realise that the solution would involve recursion, it is not possible to express this in a "pure" regex engine (in practice, most regex engines have some sort of extension to allow limited violation of this rule).

Since you're writing in a Java context - why not use the nice programming language to perform this fairly simple manipulation of a string, rather than trying to shoehorn a regex into the solution? :)

I'm not sure of what you really regarding your question want but with the given case :

"boo(a,b), c".split("(?<=\\)),") //look for a ',' preceeded by ')'

gives you an array containing ["boo(a,b)","c"] .

EDIT :

For this case : boo(a(a,b),b),c , use :

"boo(a(a,b),b),c".split("(?<=\\)),(?!(\\w\\p{Punct}?)+\\))")

(?!(\\\\w\\\\p{Punct}?)+\\\\)) means you don't accept match if it's followed by (a word and one or zero punctuation) n times , and a right parenthesis .

The result is an array containing ["boo(a(a,b),b)","c"] .

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