热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

匹配某物吗?-Matchanythingthatisasomething?

Withregexhowcanamatcheverythinginastringthatisntsomething?Thismaynotmakesensebutr

With regex how can a match everything in a string that isnt something? This may not make sense but read on.

使用regex,如何将不属于某个内容的字符串中的所有内容匹配起来?这可能说不通,但请继续读下去。

So take the word baby for instance to match everything that isn't a b you would do something like [^b] and this would match a and y. Simple enough! But how in this string Ben sits on a bench can I match everything that isn't ben so i would be attempting to match sits on a ch?

所以比如婴儿这个词匹配的一切不是你会做类似[^ b]这将匹配和y。很简单!但是本坐在板凳上,我怎么能把不是本的东西都匹配起来呢?

Better yet match everything that isn't a pattern? e.g. in 1a2be3 match everything that isn't number,letter,number, so it would match every combination in the string except 1a2?

更好的是匹配所有不是模式的东西?例如,在1a2be3中匹配所有不是数字,字母,数字的东西,所以它会匹配字符串中的所有组合,除了1a2?

6 个解决方案

#1


1  

(?:ben)|(.)

What this regex does is match ben or any other character, however, ben isn't captured but the other characters are. So you'll end up with a lot of matches except for the ben's. Then you can join all those matches together to get the string without the ben's.

这个regex所做的是匹配ben或任何其他字符,但是ben没有被捕获,但是其他字符被捕获。所以你会有很多比赛,除了本的。然后你可以把所有的火柴合在一起,得到没有本的绳子。

Here an example in python.

这里是python中的一个例子。

import re

thestr = "Ben sits on a bench"
regex = r'(?:ben)|(.)'

matches = re.findall(regex, thestr, re.IGNORECASE)
print ''.join(matches)

This will ouput:

这将输出:

 sits on a ch

Note the leading space. You can of course get rid of that by adding .strip().

注意的主要空间。当然,可以通过添加.strip()来消除这一点。

Also note, that it is probably faster to do a regex that replaces ben with an empty string to get the same result. But if you want to use this technique in a more complex regex it could come in handy.

还需要注意的是,使用一个regex用一个空字符串替换ben以获得相同的结果可能会更快。但如果您想在更复杂的regex中使用这种技术,它可能会派上用场。

And of course you can also put more complex regexes at the place of ben, so for example your number,letter,number example would be:

当然,你也可以把更复杂的regexes放在ben的位置,例如你的数字,字母,数字例子是:

(?:[0-9][a-z][0-9])|(.)

#2


1  

Short answer: You can't do what you're asking. Technically, the first part has an ugly answer, but the second part (as I understand it) has no answer.

简短的回答:你不能做你要求的。从技术上讲,第一部分有一个丑陋的答案,但是第二部分(我理解)没有答案。


For your first part, I have a pretty impractical (yet pure regex) answer; anything better would require code (like @rednaw's much cleaner answer above). I added to the test to make it more comprehensive. (For simplicity, I'm using grep -Pio for PCRE, case insensitive, printing one match per line.)

对于你的第一部分,我有一个非常不切实际(但纯粹的正则表达式)的答案;任何更好的方法都需要代码(比如@rednaw上面的更简洁的答案)。我增加了测试,使它更全面。(为了简单起见,我在PCRE中使用grep -Pio,不区分大小写,每行打印一个匹配。)

$ echo "Ben sits on a bench better end" \
    |grep -Pio '(?=b(?!en)|(?

I'm basically making a special case for any letter in "ben" so I can include only iterations that are not themselves part of the string "ben." As I said, not really practical, even if I am technically answering your question. I've also saved a blow-by-blow explanation of this regex if you want further detail.

我基本上为“ben”中的任何字母都做了一个特殊的情况,所以我只能包含不属于字符串“ben”的迭代。就像我说的,不太实际,即使我严格地回答了你的问题。我还保存了这个regex的详细说明。

If you're forced into using a pure regex rather than code, your best bet for items like this is to write code to generate the regex. That way you can keep a clean copy of it.

如果您被迫使用纯regex而不是代码,您最好的选择是编写代码来生成regex。这样你就可以保留一个干净的副本。


I'm not sure what you're asking for the remainder of your challenge; a regex is either greedy or lazy [1] [2], and I don't know of any implementations that can find "every combination" rather than merely the first combination by either method. If there were such a thing, it would be very very slow in real life (rather than quick examples); the slow speed of regex engines would be intolerable if they were forced to examine every possibility, which would basically be a ReDoS.

我不确定你对剩下的挑战有什么要求;regex要么是贪婪的,要么是懒惰的[1][2],我不知道有任何实现可以找到“每个组合”,而不是通过任何一种方法找到第一个组合。如果有这样的东西,在现实生活中它会非常缓慢(而不是快速的例子);regex引擎的缓慢速度将是无法忍受的,如果他们被迫检查每一种可能性,这基本上是一个重dos。

Examples:

例子:

# greedy evaluation (default)
$ echo 1a2be3 |grep -Pio '(?!\d[a-z]\d)\w+'
a2be3

# lazy evaluation
$ echo 1a2be3 |grep -Pio '(?!\d[a-z]\d)\w+?'
a
2
b
e
3

I assume you are looking for 1 1a a a2 a2b a2be a2be3 2 2b 2be 2be3 b be be3 e e3 3 but I don't think you can get that with a pure regex. You'd need some code to generate every substring and then you could use a regex to filter out the forbidden pattern (again, this is all about greedy vs lazy vs ReDoS).

我猜你是在寻找1a a a2 a2b a2be3 2b 2be3 b be be3 e3但我认为你不可能用一个纯的regex得到它。您需要一些代码来生成每个子字符串,然后您可以使用regex来过滤禁止的模式(同样,这都是关于贪婪vs lazy vs ReDoS)。

#3


0  

If you want to match all the words except one, you can use negative lookahead: \b(?!ben\b)\w*\b, but for an answer to your exact question Jon's comment seems the simplest.

如果你想要匹配除一个以外的所有单词,你可以使用消极的前视:\b(?!ben\b)\w*\b,但是要想回答你的问题,Jon的评论似乎是最简单的。

#4


0  

Okay The simplest thing To Do is Match Everything

最简单的方法就是匹配所有的东西

(.*?)

Then on the pattern matched do another Match for What you don't want(for e.g In perl you will have the pattern matched in the variable $&).

然后在匹配模式上为你不想要的做另一个匹配。在perl中,您将在变量$&中匹配模式。

If it matches, That's not what you want else you have your match.

如果匹配,那不是你想要的,否则你就有匹配的了。

Simple A-B where A is everything(.*?) and B is What you don't want.So you end up doing two matches but i think that's fine.

简单的A-B, A代表一切,B代表你不想要的。所以你最后做了两场比赛,但我认为那很好。

#5


0  

Just replace everything that matches your pattern with a blank (to delete it).

只需将与您的模式匹配的所有内容替换为空白(以删除它)。

You haven't indicated what language you are using, so genetically:

你还没有指出你在用什么语言,所以从遗传学上来说:

s/ben//g

and your other example:

和你其他的例子:

s/\d[a-zA-Z]\d//g

#6


0  

If you want list of strings, use "split on regexp" instead of "match on regexp".

如果您想要字符串列表,请使用“在regexp上拆分”而不是“在regexp上匹配”。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • centos安装Mysql的方法及步骤详解
    本文介绍了centos安装Mysql的两种方式:rpm方式和绿色方式安装,详细介绍了安装所需的软件包以及安装过程中的注意事项,包括检查是否安装成功的方法。通过本文,读者可以了解到在centos系统上如何正确安装Mysql。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
author-avatar
小蚊子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有