in reply to How regex works here?

In the first regex you are matching only the "B6y green" and you are replacing that again with "B6y green". So the $lines is same as it is

In the second regex you are matching "B6y green" and the remaining string and you are replacing that with "B6y green". So the $lines has only the matched pattern.

--Lakshmanan G.

The great pleasure in my life is doing what people say you cannot do.


Replies are listed 'Best First'.
Re^2: How regex works here?
by nagalenoj (Friar) on Apr 08, 2009 at 05:38 UTC

    yeeeah..! Fine.. I understood.,

    So the problem is not with the matching and with the substitution.

    Thanks..!
      Yes.

      There's no need at all to change the original string to extract the data you want. All you need is to match it.

      if($line =~ /^(\S+\s+\S+)/) { $match = $1; }
      or even simpler:
      ($match) = $line =~ /^(\S+\s+\S+)/;
      Note that for the latter, the assignment must be in list context, not scalar context; that's what the parens around the variable on the left hand side are for.