get words from text by regexp

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
frosty3d
Forum Newbie
Posts: 4
Joined: Wed May 16, 2007 8:44 am

get words from text by regexp

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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";
}
frosty3d
Forum Newbie
Posts: 4
Joined: Wed May 16, 2007 8:44 am

Post by frosty3d »

Thank you very much! :)
frosty3d
Forum Newbie
Posts: 4
Joined: Wed May 16, 2007 8:44 am

Post by frosty3d »

Thank you very much! :)
Post Reply