How to exclude multiple words from an existing RegEx template

I have the following pattern that matches placeholders such as ( %s , %16.6fAl , %d , etc.):

%([az]+)\b|%([a-z0-9\.]{2,20})\b

However, I need to ignore the following placeholders:

%w %1w %2w %3w %O %M

I tried to look and check out the forum, but I'm afraid my regular expressions are limited. Is there anyone there who can have a solution?

+6
source share
3 answers

If you want to match all placeholders except five very specific ones, and your code allows this, the easiest way is probably to combine all placeholders first, and then (if they match) use another regular expression to check the five โ€œforbiddenโ€ ones and ignore them. A RegExp entry that matches %d , %1d , %4d , %4w but doesn't match %1w will be ... interesting. Of course, maybe just not fun.

+1
source

Perhaps you will try this. It executes your given examples.

% ([a-LNP-yy-g] +) \ b |% ((?: [0-9] [^ ] {1,19}) | (([. AZ] {2,20})) ) \ B

+1
source

I think you may want to match what you really want, rather than trying to exclude what you don't want.

Here is a rough regexp for printf

  % ([+ - # 0])? (([1-9] [0-9] *) | \ *)? (\. ([1-9] [0-9] *) | \ *)? ([hljztL] | hh | ll)? [diuoxXfFeEgGaAspn] 
0
source

All Articles