0

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.

7
  • 1
    Just find ), and before that is the method and after is the extra stuff? Commented May 3, 2012 at 15:00
  • Are you looking at plaintext, the .class file, or are you reading it in? Commented May 3, 2012 at 15:00
  • the problem is that it has to be recursive. i.e boo(a(a,b),b),c should split by the "," before the c, only find the "highest level" ','s The String is read from a file, manipulated and is in a String object Commented May 3, 2012 at 15:03
  • 1
    Instead RegEx you should try parsing it through a Stack. Commented May 3, 2012 at 15:05
  • @Lablabla If that's the case, you should probably edit your post because you did not mention anything of this sort. Also, how would you ever get a method call to look like that in the first place? Commented May 3, 2012 at 15:05

2 Answers 2

4

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? :)

Sign up to request clarification or add additional context in comments.

3 Comments

+1, for this clear approach. May or may not be it to be done in RegEx is another matter.
it's a about regexes. we could use other methods, however since the homework are all about regexes, if it's possible, I'd rather do it this way
Fair enough (though I think the question would be better if you emphasize that you're specifically thinking about regexes for educational purposes, instead of merely thinking it's a practical way to solve this problem). Though I stand by my answer - strictly you can't do it with regexes, but I'm sure Java's regex engine has something in to cope with this. I leave it to others to point out what that might be! Maybe "no" really is the correct answer in the context of this homework.
0

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"].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.