如何查找与正则表达式不匹配的所有内容 [英] How to find everything that is not matching a regular expression

查看:695
本文介绍了如何查找与正则表达式不匹配的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索成千上万的HTML代码,以了解高度,宽度或任何其他CSS的不良习惯。



例如,我想获得所有地方其中height不提供单位,例如 height:40 应该被找到,但 height:40px



因为我使用的是搜索程序代理ransack,我可以把正则表达式在文件中搜索。



目前我的正则表达式是:

 (height:)[\ s] * [0-9] \。?[0-9] +(px)

c $ c> height:40px 。 (以后我想添加宽度或其他东西)



我的问题是如何使一个不是最好的?


解决方案

使用负前瞻功能(?! regex),例如:

  height:\s * \d +(?: \.\d +)?(?! px | \d)



需要

\d 才能防止回溯替代品匹配。


I would like to search all over thousands of HTML code for bad practice of height, width or any other CSS.

for instance I would like to get all places where height is not provided with units, for instance height:40 should be found, but height:40px shouldn't.

For that I am using the search program agent ransack, in which I can put regular expression to search within files.

Currently my regular expression is:

(height:)[\s]*[0-9]*\.?[0-9]+(px) 

this finds everything that is like height:40px. (Later on I want to add width, or other things)

My question is how to make a NOT on top of all that?

Or is there any other good application to search files for regular expressions?

解决方案

Use a negative lookahead (?!regex), eg:

height:\s*\d+(?:\.\d+)?(?!px|\d)

\d is needed to prevent backtracking alternatives from matching.

这篇关于如何查找与正则表达式不匹配的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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