Page 1 of 1

get words from text by regexp

Posted: Wed May 16, 2007 8:52 am
by frosty3d
Hi!

Could somebody help me with regexp? :)

I have text like this

selection 1
67-98
end selection
selection 2
49-66
end selection
selection 3
1-48
end selection
selection very_long_name_of_selection_set
1-48
end selection


I have to get words [1,2,3,very_long_name_of_selection_set] by perl script.
I know how I can get full strings [selection 1,selection 2,selection 3,selection very_long_name_of_selection_set]
But I need only 1 word after "selection"
Wich regexp can do it?

Thank you!

Alex

Posted: Wed May 16, 2007 10:07 am
by Chris Corbyn
Sounds like a homework assignment:

Code: Select all

#!/usr/bin/perl

$str = "selection 1
67-98
end selection
selection 2
49-66
end selection
selection 3
1-48
end selection
selection very_long_name_of_selection_set
1-48
end selection";

while ($str =~ /^selection (\S+)/mg)
{
  print $1;
  print "\n";
}

Posted: Thu May 17, 2007 1:38 am
by frosty3d
Thank you very much! :)

Posted: Thu May 17, 2007 1:39 am
by frosty3d
Thank you very much! :)