0

Replacement with Regular expressions in Java - I have a String

    allof{
    condition {licenseState=="NY"}
    condition{professionId==301}
    condition  {professionId=="301"}
}

would be it's possible to do a replacement in java using regex so the string looks like this one:


    allof{
    condition 'licenseState=="NY"', {licenseState=="NY"}
    condition 'professionId==301', {professionId==301}
    condition 'professionId=="301"', {professionId=="301"}
    }  

basically getting what inside the {} brackets and putting it separately. Is it possible and how? NewLine char is not guaranteed to be present after each condition.

I've tried:

condition\s*?{[a-zA-Z0-9=<>\s"']*} 
4
  • 1
    Yes it is possible, what have you tried? Commented Mar 29, 2013 at 18:49
  • I have tried code condition\s*?{[a-zA-Z0-9=<>\s"']*} code which selected everything, but I don't know how to extract inner part of it Commented Mar 29, 2013 at 18:51
  • Here is a site that you can use to test out your regex: regexplanet.com/advanced/java/index.html Go there an try your regex Commented Mar 29, 2013 at 18:53
  • @eugened this is a very simple case for group reference. do some google, do some test. next time you met it again, you know how to handle. e.g. this could let u start : {([^}]*) Commented Mar 29, 2013 at 18:54

2 Answers 2

1

You can use the string replaceAll function. Something like this:

yourString.replaceAll("\\{([^{}]*\\=\\=[^{}]*)\\}", "'$1', {$1}");
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming no nested {}

str = str.replaceAll( "(?<=condition)\\s*(\\{([^}]+)\\})", " '$2', $1" );

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.