^p vs. [^p] in Unix file using UE regex

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

^p vs. [^p] in Unix file using UE regex

Postby SAbboushi » Mon Jun 18, 2012 9:09 pm

According to documentation I have read, ^p is UE regex I can use in place of ^r^n

I have UNIX PHP files (i.e. lines are terminated with \n). I just realized that a find operation for ^p results in nothing found, whereas a UE regex find for [^p] finds all the \n characters.

Is this by design? Am I missing something?
User avatar
SAbboushi
Basic User
Basic User
 
Posts: 38
Joined: Sun Nov 07, 2004 12:00 am

Re: ^p vs. [^p] in Unix file using UE regex

Postby Mofi » Tue Jun 19, 2012 1:11 am

Well, ^p matches only DOS line terminators - carriage return + line-feed. ^p is short for ^r^n. That explains also why [^p] finds also Unix line terminators as this is resolved internally to [^r^n] and means now either carriage return or line-feed. Better would be [^p]+ as this results internally in [^r^n]+ and therefore matches now also DOS line terminators completely and not just the carriage return of a CR+LF pair. But care must be taken as [^p]+ matches now also multiple line terminators of any type.

^n should be used for UNIX line terminators as this special character matches only the line-feed.

The internal replacement of [^p] by [^r^n] explains also why ^p used in a function string regular expression in a wordfile results in working for DOS, UNIX and MAC files taking into account that ususally after ] there is either + or * too.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: ^p vs. [^p] in Unix file using UE regex

Postby SAbboushi » Tue Jun 19, 2012 9:15 am

Thanks - now I get it.
User avatar
SAbboushi
Basic User
Basic User
 
Posts: 38
Joined: Sun Nov 07, 2004 12:00 am


Return to Find/Replace/Regular Expressions

cron