perlinit has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to do what appears to be a simple regex however I just can't seem to get it to work. Does anyone have any suggestions? I need to remove these tags where there is no text between them.
$_ =~ s/<IT><\/IT>//gi;
Thanks
Phil

Replies are listed 'Best First'.
Re: Regular Expression
by pbeckingham (Parson) on Apr 19, 2004 at 19:26 UTC

    Check for whitespace perhaps? Do those tags perhaps span lines? Does $_ still contain what you expect it to contain?

    $_ =~ s/<IT>\s*<\/IT>//gi;
    Update: Thanks you ysth for the correction.

      That worked. I tried everything but that. I didn't try that because the is not any whitespace there. Oh well. Thank You Phil
Re: Regular Expression
by dug (Chaplain) on Apr 19, 2004 at 18:53 UTC
    Is there whitespace between those tags you are trying to strip?

    -- Douglas
      That is the $20000 question. There is nothing at all between the <IT></IT>.
Re: Regular Expression
by CountZero (Bishop) on Apr 19, 2004 at 19:35 UTC
    What error do you get? Or is it just doing nothing at all?

    Perhaps you can show us a part of the input?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Regular Expression
by ltcmus (Initiate) on Apr 20, 2004 at 04:07 UTC
    Did you try escaping the < or the >?

    $_ =~ s/\<IT\>\<\/IT>//gi works for me.

    Danny

      You don't need to escape the angle brackets. Escaping a regular character has no effect at all. It will match the same as without the escape.

Re: Regular Expression
by Util (Priest) on Apr 20, 2004 at 17:16 UTC

    If the earlier replies did not resolve the problem, then something besides the RE is faulty.

    Perhaps the output you are examining did not come from the result of the RE, due to an intervening localization of $_. Also, some output (NUL, etc.) is not visible in some terminals or editors.

    Try dumping $_ immediately before and after the RE. Data::Dumper, with its 'qq' mode turned on, will expand any oddball characters into readable forms.

    #!/usr/bin/perl -w use strict; use Data::Dumper; $Data::Dumper::Useqq = 1; $_ = join '', <DATA>; print "\nBefore: ", Dumper($_); $_ =~ s/<IT>\s*<\/IT>//gi; print "\nAfter: ", Dumper($_); __DATA__ Foo<iT></It>Bar Baz<it>!</it>Qux
    Output:
    Before: $VAR1 = "Foo<iT></It>Bar\nBaz<it>!</it>Qux\n"; After: $VAR1 = "FooBar\nBaz<it>!</it>Qux\n";