regex - How negative lookbehind works in below example? -


why on applying regular expression(rx) on data(d) gives output(o) ?
regular expression (rx):

s/(?<!\#include)[\s]*\<[\s]*([^\s\>]*)[\s]*\>/\<$1\>/g 

data (d):

#include  <a.h>  // 2 spaces after e 

output (o):

#include <a.h>  // 1 space still there 

expected output is:

#include<a.h>  // no space after include 

the condition (?<!\#include) true you've passed first of 2 spaces, therefore match starts there.

#include  <a.h>          ^^^^^^- matched regex. 

that means space not removed replace operation.

if use positive lookbehind assertion instead, desired result:

s/(?<=#include)\s*<\s*([^\s>]*)\s*>/<$1>/g; 

which can rewritten use more efficient \k:

s/#include\k\s*<\s*([^\s>]*)\s*>/<$1>/g; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -