"Proximity" in searches

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

"Proximity" in searches

Postby Pedrober » Tue Sep 11, 2007 8:03 am

Is it possible to search expression A AND expression B in a proximity range? What I mean is, for example, "text A" AND "text B" in the same paragraph or in a proximity range (for example: 80 characters).
Best regards.
User avatar
Pedrober
Basic User
Basic User
 
Posts: 23
Joined: Wed Aug 22, 2007 11:00 pm

Re: "Proximity" in searches

Postby jorrasdk » Tue Sep 11, 2007 8:18 am

Well I think these suggestions come close to what you want to achive:

Using Perl regexp this searches for "text A" and "text B" in the same paragraph:

text A[^\r\n]+text B
([^\r\n]+ equals everything but CR and LF which separates paragraphs).

Proximity could look something like:

text A.{1,80}text B
(.{1,80} = any chars from 1 to 80 occurence (greedy)).

(And remember to check "regular expressions" in the find dialog and activate Perl Regexp in the configuration)
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: "Proximity" in searches

Postby Pedrober » Tue Sep 11, 2007 8:43 am

Thank you very much! Definitively I have to study the Perl search syntax. It's really powerful.
User avatar
Pedrober
Basic User
Basic User
 
Posts: 23
Joined: Wed Aug 22, 2007 11:00 pm

Re: "Proximity" in searches

Postby pietzcker » Tue Sep 11, 2007 8:57 am

Very good solution. However, the "proximity" example has one problem: It will not match if the two occurences are separated by a CR/LF. As far as I know, there is no way to tell UE'S Perl regex engine to allow the dot (.) to also match newlines.

A possible workaround could be used if there is one character that you know for certain will not turn up in your files, e.g. ÿ (ASCII 255). If that's the case, you can rework the regex to

Code: Select all
text A[^ÿ]{1,80}text B

(If you also want to allow for the possibility that there are zero characters between the two texts ("textAtextB") then use * instead of + in the first regex and {0,80} instead of {1,80} in the second regex)

HTH,
Tim

P.S. If anyone can think of a more elegant solution, I'd be glad to hear about it. I wonder if it's really necessary to use such an ugly trick. Maybe it would be a sensible feature request to add the option "dot matches newline"...
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: "Proximity" in searches

Postby Pedrober » Tue Sep 11, 2007 9:12 am

It's not an ugly trick and it works perfectly!
User avatar
Pedrober
Basic User
Basic User
 
Posts: 23
Joined: Wed Aug 22, 2007 11:00 pm


Return to Find/Replace/Regular Expressions