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?

Armali
  • 18,255
  • 14
  • 57
  • 171
Sumit Gera
  • 1,249
  • 4
  • 18
  • 34

2 Answers2

0

To match a whole string that doesn't contain 101:

^(?!.*101).*$

Look-ahead are indeed an easy way to check a condition on a string through regex, but your regex will only match alphanumeric words that do not start with 101.

Robin
  • 9,415
  • 3
  • 34
  • 45
0

You wrote

I know that such expressions cannot be written using Regular Expression considering http://en.wikipedia.org/wiki/Regular_language.

In that Wikipedia article, you seem to have missed the

Note that the "regular expression" features provided with many programming languages are augmented with features that make them capable of recognizing languages that can not be expressed by the formal regular expressions (as formally defined below).

The negative lookahead construct is such a feature.

Armali
  • 18,255
  • 14
  • 57
  • 171