1

regex not working as wanted

Code example:

widgetCSS = "#widgetpuffimg{width:100%;position:relative;display:block;background:url(/images/small-banner/Dog-Activity-BXP135285s.jpg) no-repeat 50% 0; height:220px;} 

someothertext #widgetpuffimg{width:100%;position:relative;display:block;}"

newWidgetCSS = widgetCSS.replaceAll("#widgetpuffimg\\{(.*?)\\}","");

I want all occurrences in the string that match the pattern "#widgetpuffimg{anycharacters}" to be replaced by nothing

Resulting in newWidgetCSS = someothertext

5
  • I'm confused...are you saying the result of "someothertext" is what you desire, or what you're currently getting? Because I get "someothertext" as the result. Commented Nov 2, 2011 at 16:14
  • Yeah, it works fine for me; although I wasn't sure how to edit it to make it legible. I don't think you need the grouping around the .*? though. Commented Nov 2, 2011 at 16:18
  • same here, i am also getting " someothertext " as the output. Commented Nov 2, 2011 at 16:18
  • Sorry all It works as intended in my real code I was lacking the last curly braces that I included in the example above. Will remove the post. Thanks for all input as it verified that it should work as written and I had to look at the real code. Commented Nov 2, 2011 at 16:28
  • 1
    Mark an answer (possibly your own) as solution instead of appending "SOLVED" to the title. Commented Nov 2, 2011 at 16:33

2 Answers 2

1

Update: After edit of question

I think the regex is working properly according to your requirements if you are escaping your { as mentioned below. The exact output I am getting is " someothertext ".

It has to be newWidgetCSS = widgetCSS.replaceAll("#widgetpuffimg\\{(.*?)\\}",""); You need to use \\{ instead of \{ for escaping { properly.

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

7 Comments

? I am escaping with double backslashes
This was a misunderstanding due to the OP not formatting the code in the question properly.
@jalmen in your first version of the question it was showing single backslash. editing the answer now.
@Narendra Also you don't need to escape } :)
@FailedDev I think in java you need to escape { to match a literal {. Please check.
|
1

This should work :

String resultString = subjectString.replaceAll("(?s)\\s*#widgetpuffimg\\{.*?\\}\\s*", "");

Explanation :

"\\s" +                // Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   "*" +                 // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"#widgetpuffimg" +    // Match the characters “#widgetpuffimg” literally
"\\{" +                // Match the character “{” literally
"." +                 // Match any single character
   "*?" +                // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
"}" +                 // Match the character “}” literally
"\\s" +                // Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   "*"                   // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)

As an added bonus it trims the whitespace.

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.