Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a few words from an existing RegEx pattern

Tags:

regex

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

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

I do, however, need to ignore the following placeholders:

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

I've tried to look up and check the forum, but I'm afraid my regex knowledge is limited. Is there anyone out there that may have a solution?

like image 275
user1940524 Avatar asked Jan 01 '13 04:01

user1940524


2 Answers

If you want to match all placeholders except five very specific ones, and your code allows it, the easiest way is probably to first match all the placeholders, then (if matched) use another regexp to check for the five "prohibited" ones and ignore them. Writing a RegExp that will match %d, %1d, %4d, %4w but will not match %1w will be... interesting. Certainly possible, just not fun.

like image 99
Jan Schejbal Avatar answered Nov 13 '22 11:11

Jan Schejbal


Your might try this one. It fulfills your given examples.

%([a-lnp-vx-z]+)\b|%((?:[0-9][^w]{1,19})|(([a-z.]{2,20})))\b

like image 35
Afzal Naushahi Avatar answered Nov 13 '22 10:11

Afzal Naushahi