PDA

View Full Version : regular expression question



raac
September 5th, 2011, 12:55 AM
hello guys, Ive been working on a regular expression for a while...

from a set {0,1,2}, I need to create a regular expression that takes string that do not contain 010.

This is what Ive done so far

^([^0]|00*[^1]|00*1[^0]|^[0-2]$)*$


however it does not take all instances
for instance it does not take
0000110

and it suppose to.

Im not sure why is not taking that number

Thanks in advance

papibe
September 5th, 2011, 12:57 AM
Is it for sed, bash, something else?
Regards.

raac
September 5th, 2011, 01:06 AM
It has to be generic

I have to use the normal operations {*,^(negation),+,(x|x),[a-z].....etc}

raac
September 5th, 2011, 02:08 AM
Could there be an easier way to accomplish this?
It sounded simple when I thought about it, but when I sat down and started doing the regular expression I realized I was wrong

Smart Viking
September 5th, 2011, 02:18 AM
Make some lines, and say if they should match or not.
It's hard to understand what you mean.

raac
September 5th, 2011, 02:27 AM
0000000010 should NOT match because it contains 010
1111111001 should match because it does not contain 010
(empty string) should match because it does not contain 010
010 should NOT match because it contains 010
11111111101011111 should NOT match because it contains 010
0002200000000001000000 should NOT match because it contains 010
00000000000 should match because it does not contain 010

Smart Viking
September 5th, 2011, 02:39 AM
There are more intelligent ways to do that:


Example with grep:

echo "11111111101011111" | grep -v 010
Example in python:

strings = ["0000000010","1111111001","","11111111101011111","0002200000000001000000","00000000000"]

for i in strings:
if not "010" in i:
print i

Vaphell
September 5th, 2011, 02:42 AM
http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word

NovaAesa
September 5th, 2011, 04:54 AM
This looks a heck of a lot like homework to me. I would suggest building a finite state automata, then convert it into a regular expression from there if you are having trouble.