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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on why doesnt my regex doesnt matches certain character?

Replies are listed 'Best First'.
Re: why doesnt my regex match certain character?
by Roy Johnson (Monsignor) on May 11, 2005 at 11:33 UTC
    If ord is returning 0, it is a null, and you can represent the character as \0. Other characters can be represented by \### where the #s are octal digits. You can get the octal representation for some unknown $char by using sprintf "%o", ord($char)

    Caution: Contents may have been coded under pressure.
Re: why doesnt my regex doesnt matches certain character?
by pelagic (Priest) on May 11, 2005 at 10:20 UTC
    You must be more specific and show some example of your text and your code.
    Otherwise nobody will be able of helping you!

    pelagic
      Hi Pelagic

      I cant copy the sample input. The character appears as ''. I want to match this character in my regex

      Thanks in advance

      A M Angelo

        Those are control characters of some type. That particular one is "Delete" - character \x7F. Assuming they are all the same, you can match against that. It might be better to match against a character class though, in case they vary.

        Try:

        $text =~ /\p{Control}/;

        to match, or

        $text =~ s/\p{Control}//g;

        to remove.

        The trick is to determine the ascii value of the character. Once you have that you can take its octal value and use ie \035 to match for it, where /s/035/the octal value of that character/. An entirely different approach, although far less likely to work is to simply use . which will match 1 character, regardless of what it is. Ofcourse this depends very heavily on whether the text you're trying to match has a specific order or not.

        Remember rule one...