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

What do I use to say "If this string contains ANY characters" (spaces, letters, numbers, etc...)? Thanx

Replies are listed 'Best First'.
Re: Removing characters
by ishamael (Beadle) on Apr 20, 2000 at 23:43 UTC
    one would think that containing characters would mean not containing simply "", so
    if ($value ne "") { print "the value contains characters\n"; }
    or, conversly
    if ($value eq "") { print "the value contains no characters\n"; }
    if that suits your fancy.

    charlie schmidt
Re: Removing characters
by Anonymous Monk on Apr 21, 2000 at 01:12 UTC
    Sorry.... I was clear. This is was I want:

    I want to remove all html tags entered by a user. This is the code I have (which works), but I need to know what to put where it says HERE

    $string =~ s/<HERE*>//eg;

    So, what goes between the brackets, that would mean erase any html tags from the string?

      That's tricky, trying to remove HTML; but you can try to do it w/ a regex, in which case you'll want something like this:
      $string =~ s/<[^>]*>//gs;
      Or you could use
      $string =~ s/<.*?>//gs;
      The first looks between < and > for any character that's not a >, and the second looks from the first < until it finds a > (the '?' makes it a non-greedy match).

      You don't need the "e" modifier, because you're not executing any code on the right-hand side of the substitution. And you should use the "s" because HTML tags can span multiple lines, so you want the '.' to match newlines.

      If you find that the regex really isn't working well enough, take a look at HTML::Parser, HTML::TokeParser, and HTML::FormatText.

Re: Removing characters
by ChuckularOne (Prior) on Apr 20, 2000 at 23:11 UTC
    Probaly not the best way, but this will work.
    if ($value =~ m/[.|\n]+/) { do_something; }
    It means (.) any char (\n) new line (+) require at least one

    Your Humble Servant,
    -Chuck
Re: Removing characters
by btrott (Parson) on Apr 20, 2000 at 23:21 UTC
    As you've defined characters, it seems that a string that doesn't contain any characters will be an empty string. Is that correct?

    If so, you can use

    if ($value eq "") { # contains no characters }
    If that's not what you meant, try clarifying what you mean *exactly* by characters.
Re: Removing characters
by infinityandbeyond (Sexton) on Apr 21, 2000 at 00:33 UTC
    If "no characters" could be equivalent to a null string, ie:
    $string = "";
    then perl will return "false" for any scalar with a null value. Just test for true or false on the value directly.
    if ($string) { # ... do something }
      But will also fail if $string = "0"