Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hi,

I am not able to understand the complete functioning of period (.) operator in Regular Expression under unix terminal..

It says it matches any single character.. When i gave grep 'c.t' a.txt, it return results such as cat, cbt, cmt etc.. Should it pring any other type of data too??? as i am not clear with the basics of this period operator.

What if i give grep 'c[abm].t' a.txt, it is not giving the expected results i.e. I thought it should print only cat or cbt or cmt but it prints only caat, which was one of the data. Please clarify.

Also if there is any tutorial, wherein I can master the skills of Regular Expression, then please share.. Thanks a lot.. Please try and solve my query..

Thanks.

share|improve this question

4 Answers 4

If you don't have time for a whole book on the subject, it might be handy to try out expresso (.net syntax) or the commercial tool RegexBuddy that does perl, ruby, java, .net and grep syntaxes (among others). Both tools let you test what will and what won't match, and will show you a tree of what the regular expression means.

For your question, your expected behavior would come from c[abm]t not c[abm].t but that's already been pointed out.

share|improve this answer

In your example, grep 'c[abm].t' a.txt will output any lines from a.txt that contain at least four characters that you specify. a 'c', either an 'a', a 'b' or a 'm', 'any character', and then a 't'.

'c[abm].t' will match:

cart
caat
ca%t
caNt
cm_t
moo cbqt this whole line is returned because it contains a match
rocbtt

etc, etc.

Hopefully that helps you to understand your example.

Regular expressions can be incredibly complex. I can't recommend anything offhand other than what I'd find and be looking at for the first time right now.

Be aware that there are many different flavors and implementations, from grep (and egrep / grep -E) to perl to php to Javascript, etc.

As you play with them, you'll find yourself having to remember to properly escape some things from your shell, and excape other things from the regex itself... where you want to match a literal '.' (period), you'd need to grep 'moo\.' to match 'moo.' and not 'moof'

The book Mastering Regular Expressions from O'Reilly is considered quite definitive.

share|improve this answer
    
"Be aware that there are many different flavors and implementations, from grep (and egrep / grep -E) to perl to php to Javascript, etc." That is probably one of the first things anyone trying to learn regex should know :-) –  Kyle Brandt Aug 9 '09 at 17:35

The period operator matches any character, just as the description says. So 'c.t' will match 'cat', 'cbt', 'cct' ... 'czt', 'c1t' ... etc.

In your second example the result is just as expected. [abm] matches any of the characters a, b, or m. Following that, the period operator matches any character. So the expression is looking for four letter strings only.

I've found the best resource for learning regular expressions intimately is Mastering Regular Expressions from O'Reilly Media. It covers not only a large number of regexp dialects but also gives a lot of insight in to how the engines actually do the processing. An invaluable resource if you're going to be using regular expressions regularly (pun intended).

share|improve this answer

The period operator matches any character one time and one time only. So in your first example it matched a c followed by one character that was anything followed by a t.

In your second example your pattern is told to match 4 and exactly 4 characters. They are:

c

followed by

[abm] - any one of these

followed by

. - any single character

followed by

t

As for learning Regexes, check here: http://stackoverflow.com/questions/4736/learning-regular-expressions

share|improve this answer
    
thanks a lot,, ur explanation cleared my concepts for . operator –  RBA Aug 9 '09 at 17:12
    
Pedantic: Generally any character but a newline character, '\n'. –  Kyle Brandt Aug 9 '09 at 17:43
    
Unless you enable multiline depending on the engine but in general you are correct. –  EBGreen Aug 9 '09 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.