0

I have a string as below.

$Alarm:com.Alarm(a  ==  123 || (count  ==  12345 , time  matches  "24"))

whenever i encounter the above string i need to generate the following string.I mean i need to append the string "from Stream" as below.

$Alarm:com.Alarm(a  ==  123 || (count  ==  12345 , time  matches  "24")) from Stream.

I am currently using the following pattern to acheive the same in java.

Pattern eventPattern = Pattern.compile(".*?\\.Alarm\\(.*?\\)");

But i am getting the following output.

$Alarm:com.Alarm(a  ==  123 || (count  ==  12345 , time  matches  "24") from Stream )

Please provide me some pointers to acheive the correct output.The regular expression should consider only the last paranthesis.

1
  • Can you show some code on how you are using your eventPattern? Commented Apr 11, 2012 at 11:25

1 Answer 1

1

You need to include parens matching in your Pattern. Something like the following:

Pattern eventPattern = Pattern.compile(".*?\\.Alarm\\(([^\\(]*?|\\([^\\)]*?\\))*\\)");

Things up to and including the first open parens: .*?\\.Alarm\\(

Stuff outside any internal parens: [^\\(]*?

Internal parens pair: \\([^\\)]*?\\)

Match any number of stuff outside parens or within a parens pair: ([^\\(]*?|\\([^\\)]*?\\))*

This RegexPlanet site is a great place to play with regexes to see what will work.

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

3 Comments

Which will effectively only work if the maximum level of nesting of brackets is known in advance. Regular expressions aren't Turing-complete...
Good point - poster needs to answer to what degree this is an issue
My requirment is irrespective of the number of parantesis[")"].The string needs to append to only after last closing paranthesis[")"].

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.