Vyacheslav Karamov wrote: > Hi All! > > If I have regular expression and some text. > How can I get positions of matches? > > my $text = <<END; > Accession Cvi-1 was identified as hypo-SA-responsive at both levels. > Cvi-1 was shown previously to be hyporesponsive to methyl jasmonate > (MeJa) according to both transcript accumulation and ozone sensitivity > (Rao and Davis, 1999; Rao et al., 2000). MeJa and SA signaling are > generally assumed to be mutually antagonistic, so it is interesting to > observe an accession that is hyporesponsive to both hormones (Kunkel and > Brooks, 2002). > END > > $regex = qr/\(.*?\d+?.*?\)|\[.*?\d+?.*?\]/msx
You don't need the /m modifier as you are not using ^ or $ in your regex; you don't need the /x modifier as you are not using whitespace for layout; the optional \d+ match will have no effect; and I presume you want to search for the first closing parenthesis after the opening one, rather than the last one? > my @matches = $text =~ /$regex/g; > foreach my $arr (@matches) > { > print "$arr\n" if defined $arr; > } You can retrieve the start and end positions of the last successful matches from the @- and the @+ arrays. The start and end of $1 is in $-[0] and $+[0] for instance. Like this my $regex = qr/\([^)]+\)|\[[^]]+\]/s; while ($text =~ /($regex)/g) { print "$-[0] - $+[0] = $1\n"; } HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/