简体   繁体   English

如何从现有RegEx模式中排除一些单词

[英]How to exclude a few words from an existing RegEx pattern

I have the following pattern that matches placeholders such as ( %s , %16.6fAl , %d , etc.): 我有以下与占位符匹配的模式,例如( %s%16.6fAl%d等):

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

I do, however, need to ignore the following placeholders: 但是,我确实需要忽略以下占位符:

%w %1w %2w %3w %O %M %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? 是否有人可以解决?

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. 编写将匹配%d%1d%4d%4w但不匹配%1w的RegExp将很有趣。 Certainly possible, just not fun. 当然可能,只是不好玩。

Your might try this one. 您可以尝试这个。 It fulfills your given examples. 它满足您给出的示例。

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

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 这是printf的大致正则表达式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM