Jrakru56
Jrakru56

Reputation: 1321

negative lookahead of special combination of characters in RegEx

I am trying to add \s+ around special characters, except for << or >>. For example << ) < <? ? should become << \s+)\s+ \s+<\s+ \s+<?\s+ \s+?\s+

I am using ^[\w\s]+ to capture non-word character except whitespaces but I dont seem to be able to get the negative lookahed to work. Any help?

This is what i am trying in SAS:

Data _NULL_;
a = prxchange("s/(?!(<<|>>)(^[\w\s]+)/\s*$1\s*/", -1,"<< ) < <? ?");
putlog a;
run;

Upvotes: 1

Views: 200

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use

"s/(?<!\S)(?!(?:<<|>>)(?!\S))([^\w\s]+)(?!\S)/\s+$1\s+/"

The pattern matches:

  • (?<!\S) - a left-hand side whitespace boundary
  • (?!(?:<<|>>)(?!\S)) - a negative lookahead that fails the match if, immediately to the right, there is << or >> followed with a whitespace boundary
  • ([^\w\s]+) - one or more chars other than word and whitespace chars
  • (?!\S) - a right-hand whitespace boundary.

Upvotes: 1

Related Questions