Welcome to Dream.In.Code
Become an Expert!

Join 149,582 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,774 people online right now. Registration is fast and FREE... Join Now!




regexp question

 
Reply to this topicStart new topic

regexp question, this regexp doesn't seem to do what I expect it to, why?

ericode
14 Oct, 2007 - 09:36 PM
Post #1

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
I have a file named bintxt.txt with the binary string representations of the numbers 0 through 600, each on a separate line.

When I run the following command (in Linux), I get no results
grep '^1*(01)*0*$' bintxt.txt

And fittingly, when I run the following command, I get all the lines in the file
grep --invert-match '^1*(01)*0*$' bintxt.txt

I thought that this regular expression would match lines consisting of any number of 1's followed by any number of 01s followed by any number of 0's (for example, 0,00, 1, 101010, etc would match)

Could someone explain why the regular expression is not working as I thought it would?




User is offlineProfile CardPM
+Quote Post

ericode
RE: Regexp Question
16 Oct, 2007 - 09:43 AM
Post #2

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
QUOTE(ericode @ 14 Oct, 2007 - 10:36 PM) *

I have a file named bintxt.txt with the binary string representations of the numbers 0 through 600, each on a separate line.

When I run the following command (in Linux), I get no results
grep '^1*(01)*0*$' bintxt.txt

And fittingly, when I run the following command, I get all the lines in the file
grep --invert-match '^1*(01)*0*$' bintxt.txt

I thought that this regular expression would match lines consisting of any number of 1's followed by any number of 01s followed by any number of 0's (for example, 0,00, 1, 101010, etc would match)

Could someone explain why the regular expression is not working as I thought it would?



Found the answer to my question: the parentheses need to be escaped as so:

grep '^1*\(01\)*0*$' bintxt.txt


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month