in reply to Re: Regex match at the beginning or end of string
in thread Regex match at the beginning or end of string

Thanks for the answer, could you explain how the regex works, can't quite get my head around it.
  • Comment on Re^2: Regex match at the beginning or end of string

Replies are listed 'Best First'.
Re^3: Regex match at the beginning or end of string
by toolic (Bishop) on Feb 19, 2011 at 01:48 UTC
    That's a job for YAPE::Regex::Explain!
    The regular expression: (?-imsx:(?=^.*fred)(?=.*bill)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- fred 'fred' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- bill 'bill' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re^3: Regex match at the beginning or end of string
by wind (Priest) on Feb 19, 2011 at 01:31 UTC

    He used positive look ahead assertions to test if the two patterns 'fred' and 'bill' were both in the string being matched. Essentially, it's the same thing as saying /fred/ && /bill/.

    Also, it's anchored to increase performance, since it if doesn't match at the beginning of the string, it won't match at all.

    perldoc - perlre: Just search for "Look-Around Assertions"