6

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?

  • what patterns do you want to exclude? – Naveed S Jan 01 '13 at 04:55
  • I would like to exclude the following: %w %1w %2w %3w %O %M – user1940524 Jan 01 '13 at 04:56
  • @user1940524 these could possibly be related/helpful: http://stackoverflow.com/questions/1395177/regex-to-exclude-a-specific-string-constant , http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string?lq=1 , http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word?rq=1 – d-_-b Jan 01 '13 at 05:13
  • They were helpful indeed, best I could come up with was: (?!%O)(?!%M)(?!%w)(?!%1w)(?!%2w)(?!%3w)%([a-z]+)\b|%([a-z0-9\.]{2,20})\b. This partly works, but the search still includes `%1w` `%2w` `%3w` - interestingly those with numerals only. I've come to a dead stop now, I'm afraid. – user1940524 Jan 01 '13 at 05:37
  • @user1940524 don't worry...today's a holiday, but i'm sure you'll get an answer here. just remember to check back often. – d-_-b Jan 01 '13 at 05:52
  • I think I solved it after hours: (?!%O)(?!%M)(?!%w)(?!%1w)(?!%2w)(?!%3w)%([a-z]+)\b|(?!%O)(?!%M)(?!%w)(?!%1w)(?!%2w)(?!%3w)%([a-z0-9\.]{2,20})\b - looks complicated but at a glance it seems to work! – user1940524 Jan 01 '13 at 06:05

3 Answers3

1

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.

Jan Schejbal
  • 3,820
  • 16
  • 38
1

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

Afzal Naushahi
  • 140
  • 1
  • 4
0

I think you may want to pattern match what you actually want instead of trying 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]
Jeffery Thomas
  • 40,388
  • 8
  • 88
  • 114