Find start of line over two consecutive lines

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

Find start of line over two consecutive lines

Postby sduraybito » Fri Dec 28, 2012 11:36 am

I have an array with mostly 6 lines:

01 ...
02 ...
03 ...
04 ...
05 ...
06 ...
01 ...
02 ...

etc.

Every so often the sixth line is missing:

01 ...
02 ...
03 ...
04 ...
05 ...

01 ...

I want to find the five line sets so I tried to Find %05%01 but I get no results.

How do I find start of lines over two consecutive lines?
sduraybito
Newbie
 
Posts: 2
Joined: Tue Apr 24, 2012 11:01 pm

Re: Find start of line over two consecutive lines

Postby Mofi » Sat Dec 29, 2012 9:08 am

The UltraEdit regular expression character % for beginning of a line can be used only once in a search string at beginning.

The right UltraEdit regular expression for a file with DOS line terminators for your use case would be %05*^p01

But there is a problem with this expression. It matches also

05 ...
06 ...
01


although it should match only

05 ...
01

UltraEdit regular expression character * should match any number of occurrences of any character except newline characters. And this works, but not if * is left of ^p in a search string. Then the expression * becomes greedy and matches also newline characters. I know about this bug in the UltraEdit regular expression engine very well.

However, there is a very simple workaround for this bug: instead of * the expression ?++ is used which means also zero or more occurrences of any character except newline characters and which does not have the unwanted greedy behavior.

So %05?++^p01 is the UltraEdit regular expression search string you need for this task.


Alternatively the UltraEdit regexp search strings %05[~^p]++^p01 or %05[~^r^n]++^p01 could be used too.
[~^p]++ respectively [~^r^n]++ means NOT a carriage return or line-feed character zero or more times which is equal the expression ?++.


The search string would be ^05.*\r\n01 or alternatively ^05[^\r\n]*\r\n01 with the Perl regular expression engine.


Either the search string ^05[^\r\n]*\r\n01 or the string ^05[^\p]*\p01 must be used with the legacy Unix regular expression engine.
The Unix regexp search strings ^05.*\r\n01 or ^05.*\p01 show the same greedy behavior as UltraEdit regexp search string %05*^p01.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find start of line over two consecutive lines

Postby sduraybito » Wed Jan 02, 2013 7:58 pm

Bingo! The regular expression works great!

Thanks for your help!
sduraybito
Newbie
 
Posts: 2
Joined: Tue Apr 24, 2012 11:01 pm


Return to Find/Replace/Regular Expressions

cron