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

Hello,

I am having difficulty with regular expressions. I cannot seem to make the following statements work as I desire. So as a result, I introduced a few extra scalars (not listed) which I do not want and wish to be rid of. The pertinent code bits are below.

($columns[0] =~ /(["\/home"]*)/) ($columns[0] =~ /(["\/mail"]*)/) ($columns[0] =~ "cat: cannot open /home/$username/$file")
If anyone has any idea how to match these expressions, I would be very appreciateive. Thanks for your time. Joe

Replies are listed 'Best First'.
Re: Fiddling around with reg-ex
by halley (Prior) on Jun 23, 2003 at 20:12 UTC
    From the lack of response so far, we're not sure what you were expecting these statements to do.

    What's in $columns[0] before you start playing with it? What do you want to extract? Are you trying to delete features from the string?

    All we might try to do, which may or may not be helpful, is tell you what your code actually does. This way, you can figure out how we're misunderstanding your problem.

    ($columns[0] =~ /(["\/home"]*)/)
    The first regular expression has one captured group, consisting of zero or more consecutive occurrences of ehmo"/ characters, such as in 'mh/ehh///e"hoe/mmm"me/o'. If found in $columns[0], such a span would be copied and assigned to $1, which you don't seem to be assigning to anything more useful. $columns[0] is not modified. The statement is missing a terminator (like a semicolon) before the next statement.
    ($columns[0] =~ /(["\/mail"]*)/)
    The second regular expression is similar; it has one captured group, consisting of zero or more consecutive occurrences of ailm"/ characters. If found in $columns[0], such a span would be assigned to $1, which you again fail to save anywhere. $columns[0] is not modified. The statement is missing a terminator (like a semicolon) before the next statement.
    ($columns[0] =~ "cat: cannot open /home/$username/$file")
    The third expression has no effect. It uses a string as a regular expression, one which would match any occurrence of a very literal error message, if found. $columns[0] is not modified. No results are captured anywhere.

    If you're looking to modify $columns[0], such as removing various substrings, you need the s/search/replace/ operator, not the plain matching /match/ operator. If you meant to match specific strings, don't put them in [...] character classes. If there won't be quotes in your original string, don't put quotes in your matching expression.

    --
    [ e d @ h a l l e y . c c ]

Re: Fiddling around with reg-ex
by pzbagel (Chaplain) on Jun 23, 2003 at 20:09 UTC

    An example and/or explanation of the input data would be helpful. Also, explain what you are actually trying to match on. At first glance, the " characters look wrong as well as the square braces around /home and /mail. Square braces denote a character class meaning any of the enclosed characters can match a single character. Probably not what you intended to do. Please look at the perlre docs.

    HTH

      I'm sorry I wasn't very explicit. Basically, these statements occur as part of an if statement and I am trying to determine if the strings "/home", "/mail", or "cat: cannot open /home/$username/$file" occur in the variable $columns[0]. If, for example, "/home" does occur in the variable as part of a string, I want to display the contents of the variable. If it does not occur, I want to display an error to the user. My problem is with the regular expression. In psudo-code, my statement would be something like:
      if (the string within $columns[0] contains the substring "/home") { take an action }
      I know that PHP can search for substrings with a function, but I am not sure how to do this in perl and am wondering if there is a function to do this or if I must use regular expressions. Thanks for your input; I'll try to make my posts a bit more explicit.

      updated 2003-06-24 by mirod: replaced [ and ] by [ and ] respectively.

        Then what you want is:
        if ($columns[0] =~ /"\/home"/) { take an action; }
        and so forth. This assumes you really want the quotes in the string; if you don't, just remove them from the regex.
        If I understand your question , you might be able to use alteration as in if ($var=~/home|mail|cat: cannot open/){do whatever}

        You might not need to match the entire last string as long as the part you match is unique. I'd try to stay away from the $username stuff since it can change

        Time flies like an arrow, fruit flies like banannas

Re: Fiddling around with reg-ex
by sgifford (Prior) on Jun 23, 2003 at 20:12 UTC
    Why don't you tell us what you want your regexes to do?

    Without knowing that, I can only guess, but my guess is it's those brackets---I do not think they mean what you think they mean.

    The brackets create a "character class" that matches any of the characters between them. The * means match zero or more characters in this class. The characters between them are ", /, h, o, m, e, so this will match an empty string (zero of these), "/home", "hm////oooooeeee////", etc.

Re: Fiddling around with reg-ex
by aquarium (Curate) on Jun 24, 2003 at 08:58 UTC
    if ($columns\0\ =~ /\/home/)
    {
    <indent>print 'matched on /home';</indent>
    }
    etc...
Re: Fiddling around with reg-ex
by aquarium (Curate) on Jun 24, 2003 at 09:00 UTC
    if ($columns[0] =~ /\/home/) { print 'matched on /home'}; }
    etc...
      This code seems to work well for my purposes. The syntax is easier than I expected. Regular expressions are a bit cryptic to me, but I am quickly learning, thanks to you and many other people from Perlmonks who have given me advice. Thanks for the help.
Re: Fiddling around with reg-ex
by Anonymous Monk on Jun 23, 2003 at 20:12 UTC
    perldoc perlre