0

I was trying to write a pattern which doesn't contain binary string (let's assume 101). I know that such expressions cannot be written using Regular Expression considering http://en.wikipedia.org/wiki/Regular_language.

I tried writing the pattern for the above problem using Regular Expression though and it seems to be working.

\b(?!101)\w+\b

What I wanted to ask is that can a regular expression be written for my problem and why? And if yes, then is my regular expression correct?

4

2 回答 2

0

要匹配不包含的整个字符串101

^(?!.*101).*$

前瞻确实是一种通过正则表达式检查字符串条件的简单方法,但您的正则表达式只会匹配不以 . 开头的字母数字单词101

于 2014-03-19T20:09:17.443 回答
0

你写了

我知道考虑到http://en.wikipedia.org/wiki/Regular_language,这样的表达式不能使用正则表达式来编写。

在那篇维基百科文章中,您似乎错过了

请注意,许多编程语言提供的“正则表达式”特性都增加了一些特性,使它们能够识别不能用正式正则表达式(如下正式定义)表达的语言。

负前瞻结构就是这样一个特征。

于 2014-05-30T14:06:46.023 回答