Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple REGEX-Question

Reply
Thread Tools

Simple REGEX-Question

 
 
Reinhard Glauber
Guest
Posts: n/a
 
      01-21-2006

ok, I have read the perldoc about regex but I don't understand (maybe because I'm German )
the thing with the interrogation mark

I search for 'München' in a string and want also the 4 lines before.

I was told to use:

$html =~ /(.*\n)(?:.*\n){4}.*München /;


but why not simple say:

$html =~ /(.*\n){4}.*München /;

thanks
 
Reply With Quote
 
 
 
 
Xicheng
Guest
Posts: n/a
 
      01-21-2006
Reinhard Glauber wrote:
> ok, I have read the perldoc about regex but I don't understand (maybe because I'm German )
> the thing with the interrogation mark
>
> I search for 'München' in a string and want also the 4 lines before.
>
> I was told to use:
>
> $html =~ /(.*\n)(?:.*\n){4}.*München /;

this is not true, it returns 6 lines to $&, and the last line is
terminated with 'München '(no newline"\n"). I guess this is not what
you wanted.

> but why not simple say:
> $html =~ /(.*\n){4}.*München /;

If you need totally 5 lines exactly, and meanwhile print out all the
contents on the 'München ' line, you may try something like:
$html =~ /((?:.*\n){4}.*München.*\n)/ and print $1;
Better use backreference instead of '$&' to print your data.

Xicheng

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      01-23-2006
Xicheng <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Reinhard Glauber wrote:
> > ok, I have read the perldoc about regex but I don't understand (maybe

> because I'm German )
> > the thing with the interrogation mark
> >
> > I search for 'München' in a string and want also the 4 lines before.
> >
> > I was told to use:
> >
> > $html =~ /(.*\n)(?:.*\n){4}.*München /;

> this is not true, it returns 6 lines to $&, and the last line is
> terminated with 'München '(no newline"\n"). I guess this is not what
> you wanted.
>
> > but why not simple say:
> > $html =~ /(.*\n){4}.*München /;

> If you need totally 5 lines exactly, and meanwhile print out all the
> contents on the 'München ' line, you may try something like:
> $html =~ /((?:.*\n){4}.*München.*\n)/ and print $1;
> Better use backreference instead of '$&' to print your data.


Terminology alert!

"Backreferences" means to the use of the escapes "\1", \2", etc. inside
a regex to refer back to earlier captures in the current match. The
variables $1, $2, etc. are variously called "capture variables", "digit
variables" or other things, but "backreference" is best reserved for the
escaped form.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments