Re: How to extract certain part of an regexp?

Re: How to extract certain part of an regexp?

am 18.12.2007 17:21:16 von Stephane CHAZELAS

On Tue, 18 Dec 2007 21:07:01 +0800, Bo Yang wrote:
p,,,[
>> perl -lne 'print for (/<(\d+)>/g)[2]'
>
> I read some more document about Perl one hour ago. And I think the
>
> print for (/<(\d+)>/g)[2] can be replaced with
> print (/<(\d+)>/g)[2]
>
> because the [2] require a list context, the /<(\d+)>/g will create one
> for it and then it return the third element in the list. But is failed,
> I am wondering why? Could you please help more? Thanks very much!
[...]

Try perl -lne 'print((/<(\d+)>/g)[2])'

I use "print for" in "perl -le" by habit.

As an alternative to

perl -lne 'print for /.../g'

you can also do:

perl -ne 'BEGIN{$\=$,="\n"}print/.../g'

--
Stephane