Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

Removing blink tag - regular expression and using replaceAll()

Hello!

I am trying to removes all BLINK tags from the resource to which it is applied. So, I am using replaceAll() like this, but didn’t work.

str.replaceAll("(?!)[<blink>[</blink>]]", "");

can someone help with this?

Thanks,

Avatar of Mick Barry
Mick Barry
Flag of Australia image

try something like:

str.replaceAll("<blink>.*?</blink>", "");
Avatar of dkim18
dkim18

ASKER

No, it didn't work. when I checked the page source, it didn't remove blink tags at all.
What does your source string look like exactly?
str.replaceAll("(<blank>|</blank>)","")
Oops i considered it as <blank> instead of <blink> change accordingly..
Did you also want to remove everything wrapped by the blink tag?
Avatar of dkim18

ASKER

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
This is the source and I want to remove all blink tags.

<HEAD>
<TITLE> blink page-HW#10</TITLE>

</HEAD>

<BODY>
<CENTER>
<H1><BLINK> BLINK </BLINK></H1><BR>
<H1><blink> BLINK </blink></H1><BR>

<H1><BlInK> BLINK </BlInK></H1>

</CENTER>
</BODY>
</HTML>
mine will work
String name="<H1><BLINK> BLINK </BLINK></H1><BR>";
String raj=name.replaceAll("(<BLINK>|</BLINK>)","");
System.out.println(raj);

output will be
<H1> BLINK </H1><BR>

Is this what you are expecting?
Avatar of dkim18

ASKER

This didn't work at all...
that is strange...

str.replaceAll("(<blink>|</blink>)", "");

I also think I need to prepend the first argument with "(?!)" for a case-insensitive replacement.
try:

str.replaceAll("<[bB][lL][iI][nN][kK]>.*?</[bB][lL][iI][nN][kK]>", "");
str.replaceAll("(<[bB][lL][iI][nN][kK]>|</[bB][lL][iI][nN][kK]>)","");

this wont strip the content between the tags ...hoping that is what you are looking for
Avatar of dkim18

ASKER

This is strange. The out of the following  is exactly above source and if I put      str.replaceAll("(<blink>|</blink>)", "");
it doesn't work. If I tested this,

 String name="<H1><BLINK> BLINK </BLINK></H1><BR>";
String raj=name.replaceAll("(<BLINK>|</BLINK>)","");
System.out.println(raj);

Yes, it worked! So, why this isn't working...??
+++++++++++++++++++++++++++++++++

public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain)
      throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest)request;
    HttpServletResponse res = (HttpServletResponse)response;
   
      CharArrayWrapper responseWrapper =
        new CharArrayWrapper(res);
      chain.doFilter(req,responseWrapper);
     
     String str = responseWrapper.toString();
   // str.replaceAll("(<blink>|</blink>)", "");                  
     System.out.println(str);
The line is commented...

BTW, if you want to ignore case then you can do it using this way:
Pattern p = Pattern.compile("<blink>|</blink>", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(responseWrapper.toString());
String str = m.replaceAll("');
System.out.println(str);
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial