3
grep "^[[:space:]]*clientPort[^[:alpha:]]" zoo.cfg

This is a grep pattern to extract the line that contains the port number from the configuration file.

1) Normally in a regex, I use square brackets to match any one of the enclosing characters. Here I am seeing :alpha:. Is it same as [a-zA-Z]?

2) Why there are double square parentheses in the regex?

3) [^[:alpha:]], What does the carat symbol between square brackets mean?

The configuration file contains the line,

clientPort=2181

1 Answer 1

3

Regular expression allow you to use Bracket Expression to represent the set of collating elements. The syntax for Bracket Expression is [...], where you can place in ... any of collating elements, collating symbols, equivalence classes, character classes, or range expressions.

The one you use alpha is a character class name, made by place class name between [: and :]. So, you have used the character class expression [:alpha:] in between Bracket Expression [[:alpha:]].

The character class expression is not the same as range expression [a-zA-Z] in some locales. Here's the best example you can see:

$ LC_ALL=en_US.utf8 bash -c 'case b in [A-Z]) echo yes; esac'
yes

while using character class gave you nothing:

$ LC_ALL=en_US.utf8 bash -c 'case b in [[:upper:]]) echo yes; esac'

The caret ^, if place at the beginning of Bracket Expression will negate the match of expression. [^[:alpha:]] will match any characters, which don't belong to [:alpha:] characters class:

$ case 1 in [^[:alpha:]]) echo yes;; esac       
yes

You must log in to answer this question.