使用正则表达式查找包含一个字符串并排除第二个字符串的字符串? [英] Using Regex to find string that contains one string and excludes a second string?

查看:62
本文介绍了使用正则表达式查找包含一个字符串并排除第二个字符串的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图提出一个正则表达式表达式,该表达式匹配包含"mobile"但不包含"video"的所有字符串.我一直在努力在单个表达式中执行此操作,希望对您有所帮助./p>

I'm trying to come up with a single regex expression that matches all strings that contain "mobile" and do not contain "video" I've been struggling to do this in a single expression and would appreciate any help.

推荐答案

使用否定的前瞻断言:

^(?!.*video).*mobile

示例:

$ cat 1.txt
audio-mobile
mobile-video
mobile-video
video-mobile
videomobile
mobile
audio-mobile
audio

$ grep -P '^(?!.*video).*mobile' 1.txt
audio-mobile
mobile
audio-mobile

这篇关于使用正则表达式查找包含一个字符串并排除第二个字符串的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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