Hello John,

John W. Krahn wrote on 03.12.2004:

>Jan Eden wrote:
>> Hi,
>
>Hello,
>
>> I have lines like the following:
>> 
>> \begin{letter}{Name\\ Street\\ ZIP\\ Country}
>> \begin{letter}{Name\\ Street\\ ZIP}
>> 
>> To extract the info, I used
>> 
>> my ($data) = $file_content =~ /\\begin{letter}{(.+?)}/;
>> my ($name, $street, $zip, $country) = split /\\\\ /, $data;
>> 
>> But I wonder if this is also possible using a single regex.
>
>This appears to do what you want:
>
>$ perl -le'
>@x = ( q[\begin{letter}{Name\\\ Street\\\ ZIP\\\ Country}],
>        q[\begin{letter}{Name\\\ Street\\\ ZIP}]
>      );
>for ( @x ) {
>     print;
>     my ( $n, $s, $z, $c ) = /(?:\\begin{letter}{|)([^\\}]+)(?:}|\\+\s*)/g;
>     print for $n, $s, $z, $c;
>     }
>'

Very nice, thank you. Just to make sure I get the idea: This pattern grabs one 
or more instances of "[^\\}]+", each followed by either "}" or "\\+\s*"

First of all, I did not know regexes work like this: The first time it 
encounters the capturing parentheses, it reads "Name", and then goes on to find 
the delimiting (non-capturing) parentheses. But this shouldn't be recursive, so 
why does it find Street etc?

A second thing: What does the pipe symbol at the end of the first non-capturing 
parentheses do?

Thanks,

Jan
-- 
Common sense is what tells you that the world is flat.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to