in reply to Fiddling around with reg-ex

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

Replies are listed 'Best First'.
Re: Re: Fiddling around with reg-ex
by JoeJaz (Monk) on Jun 23, 2003 at 20:50 UTC
    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.
        This syntax worked well. It is exactly what I am looking for. Thank you very much.
      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