in reply to string selection from a character class

Let me clarify. I expect either of the following as $word in the previous code:
0
or
1 t=something
And I want to capture the word "something".

I guess I cannot capture due to Perl matching greedy?

Replies are listed 'Best First'.
Re^2: string selection from a character class
by toolic (Bishop) on Jul 12, 2007 at 15:51 UTC
    #!/usr/bin/env perl use warnings; use strict; while (<DATA>) { print $2 . "\n" if /(\d)\st=(\w+)/; } __DATA__ 0 1 t=something 2 t=3foo7bar
    Creates the following output:
    something 3foo7bar
    Is this what you are trying to achieve? In any case, you should heed Limbic~Region's advice on understanding character classes (what goes into square brackets in a regex.)
      Thx toolic

      Your solution is exactly the solution I also am using to overcome and it works enough for me.

      Before my very first posting I was doing (in a loop):

      $word =~ /^(\d)\st=(\S+)/; next if ($1 == 0); print "$2\n";
      But in this way, for $word="0", pattern was not matched so previous loop's $1 and $2 was printed. And program functioned incorrectly.

      Anyway, now I am doing:

      next if(!($word =~ /(\d)\st=(\S+)/)); print "$2\n";
      and it works ok for my task.

      I thank you for your replies.

Re^2: string selection from a character class
by moritz (Cardinal) on Jul 12, 2007 at 15:54 UTC
    You don't need character classes for that:

    m/ (\d+) \s t= (\S+) /xms

    This allows one t=something string, if you want multiple, you could do something like this:

    m/ (\d+) (?: \s t= (\S+) ) /xms