1

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.

4 Answers 4

3

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: https://stackoverflow.com/questions/4736/learning-regular-expressions

3
  • thanks a lot,, ur explanation cleared my concepts for . operator
    – RBA
    Aug 9, 2009 at 17:12
  • Pedantic: Generally any character but a newline character, '\n'. Aug 9, 2009 at 17:43
  • Unless you enable multiline depending on the engine but in general you are correct.
    – EBGreen
    Aug 9, 2009 at 18:19
1

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.

1
  • "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 :-) Aug 9, 2009 at 17:35
0

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).

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .