如何“反向匹配"用正则表达式? [英] How to "inverse match" with regex?

查看:51
本文介绍了如何“反向匹配"用正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RegexBuddy,但无论如何我都遇到了这个问题:\

I'm using RegexBuddy but I'm in trouble anyway with this thing :\

我正在逐行处理一个文件.我建立了一个线模型"来匹配我想要的.

I'm processing line by line a file. I built a "line model" to match what I want.

现在我想做一个反向匹配......即我想匹配有 6 个字母的字符串的行,但前提是这六个字母不是 Andrea,我该怎么做?

Now i'd like to do an inverse match... i.e. I want to match lines where there is a string of 6 letters, but only if these six letters are not Andrea, how should I do that?

我会编写使用这个正则表达式的程序,我还不知道是在python还是php中,我先做这件事来学习一些正则表达式:)有不同的类型行,我想使用正则表达式来选择我感兴趣的类型.一旦我得到这些行,我就必须应用其他过滤器来不匹配已知值,我需要所有其他人,而不是那个.(?!not-wanted) 工作得很好,谢谢.:-)

I'll write the program that uses this regex, I don't know yet if in python or php, I'm doing this thing first to learn some regex :) There are different types of line, I wanted to use regex to select the type i'm interested in. Once I got these lines I've to apply an other filter just to do not match a known value, I need all the others, not that. The (?!not-wanted) is working pretty fine, thank you. :-)

我希望这能澄清问题:)

I hope this clarifies the question :)

推荐答案

(?!Andrea).{6}

假设您的正则表达式引擎支持负前瞻..

Assuming your regexp engine supports negative lookaheads..

..或者您可能更喜欢使用 [A-Za-z]{6} 代替 .{6}

..or maybe you'd prefer to use [A-Za-z]{6} in place of .{6}

编辑(再次):请注意,前瞻和后视通常不是反转"正则表达式匹配的正确方法.正则表达式并没有真正设置为进行负匹配,它们将其留给您使用它们的任何语言.

Edit (again): Note that lookaheads and lookbehinds are generally not the right way to "inverse" a regular expression match. Regexps aren't really set up for doing negative matching, they leave that to whatever language you are using them with.

这篇关于如何“反向匹配"用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆