Find text x followed by text y up to 10 lines later

Find, replace, find in files, replace in files, regular expressions

Find text x followed by text y up to 10 lines later

Postby Bego » Thu Jul 23, 2009 2:26 am

Hi all,

I try to create a Perl regexp search to allocate the spots in files that contain a line with the string
Code: Select all
".ObjekteListe().Rows"

that is NOT followed by
Code: Select all
"obj.ObjektFuerBesicherung"

anywhere in the next 10 lines.

Can this be done with one regexp? Or do I need to write a fat macro?

Thanks for any tips.

Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Find text x followed by text y up to 10 lines later

Postby pietzcker » Thu Jul 23, 2009 12:23 pm

Let's try

Code: Select all
"\.ObjekteListe\(\)\.Rows".*\r\n(?!(?:(?:(?!"obj\.ObjektFuerBesicherung").)*$\r\n){10})

No time right now for an explanation, will follow up if the regex works as desired :)

Cheers,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Find text x followed by text y up to 10 lines later

Postby Bego » Thu Jul 23, 2009 2:22 pm

Hi Tim !

I tried it out but it did not find anything. So I reduced the problem a bit and cut out the " that I THINK are wrong and came to sth. like this:
Code: Select all
AAAAA.*\r\n(?!(?:(?:(?!XXXXX).)*$\r\n){10})

I tried with these testlines and found both AAAAA lines instead of only the first occurrence in line 2:

Code: Select all
   hkjh lskd hkghjjgjlks lhdhd
dfdg  AAAAA fdg d  FIND THIS LINE BECAUSE NO 5-X NEAR
blibla
blubb sdflökjsdaf iobjekt
blubb besicherung
sdkfj sdlkjaflöksja öskfdj sk
blibla
blubb sdflökjsdaf iobjekt
blubb besicherung
sdkfj sdlkjaflöksja öskfdj sk
blibla
blubb sdflökjsdaf iobjekt
blubb besicherung
sdkfj sdlkjaflöksja öskfdj sk
blibla
blubb sdflökjsdaf iobjekt
blubb besicherung
sdkfj sdlkjaflöksja öskfdj sk
   dfdg  AAAAA fdg d  DO NOT FIND THIS LINE, 5 - x is near
blibla
blubb sdflökjsdaf iobjekt
blubb besicherung
sdkfj sdlkjaflöksja öskfdj sk
g klfsdglskh XXXXX d gh jh
blibla
blubb sdflökjsdaf iobjekt

We are near ...

Gruss Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Find text x followed by text y up to 10 lines later

Postby pietzcker » Thu Jul 23, 2009 2:34 pm

Ah, you're right. There was a "double negation" that screwed it up. How about

Code: Select all
AAAAA.*\r\n(?=(?:^(?:(?!XXXXX).)*$\r\n){10})

This has one drawback: If there are less than 10 lines after AAAAA until EOF, it won't match even if there's no XXXXX in those lines.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Find text x followed by text y up to 10 lines later

Postby Bego » Thu Jul 23, 2009 2:38 pm

EOF drawback wont really mind.
But still this one finds both AAAAA ...
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Find text x followed by text y up to 10 lines later

Postby Bego » Thu Jul 23, 2009 2:43 pm

STOP !!!

Just figured out that I used the wrong hard disc having 14.20 on it.
Works well with 15.10.0.1017 I normally work on.

Thanks Tim !!
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Find text x followed by text y up to 10 lines later

Postby pietzcker » Fri Jul 24, 2009 4:02 am

The funny thing is that I think I found a bug in RegexBuddy with this regex. It highlights the second AAAAA although it shouldn't, and when I debug the regex, the debugger reports a failed match attempt. Will have to dig further into this... (and maybe find out a more elegant solution)?

Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Find text x followed by text y up to 10 lines later

Postby pietzcker » Tue Aug 11, 2009 6:49 am

There actually was a bug in RegexBuddy which is now fixed. So thanks again for providing us with that interesting problem :)
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm


Return to Find/Replace/Regular Expressions