Search code examples
javaregexreplaceall

Java regex replaceAll not working


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


Solution

  • 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.