如何使用正则表达式忽略串词 [英] How to ignore words in string using Regular Expressions

查看:100
本文介绍了如何使用正则表达式忽略串词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一根正则表达式可用于做不匹配。就拿下面的URL

I need a piece of regex that can be used to do a NOT match. Take for example the following URL's

 http://www.site.com/layout/default.aspx
 http://www.site.com/default.aspx
 http://www.site.com/layout.aspx

正则表达式应该不匹配包含目录布局

The regex should NOT match any url string that contains the directory "layout"

http://www.site.com/layout/default.aspx

和而应该匹配

http://www.site.com/default.aspx
http://www.site.com/layout.aspx

我怎样才能做到这一点使用.NET正则表达式?

How can i do this using .NET regex?

推荐答案

使用负前瞻:(?!。* /布局/)

Use negative lookahead:

^

您必须锚定在字符串的开头或者你会得到误报即(?!/布局/)单独将无法工作。

You have to anchor to the start of the string or you'll get false positives i.e. (?!/layout/) alone won't work.

这篇关于如何使用正则表达式忽略串词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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