4

Consider:

$ echo '<H1>heading</H1>' | grep '<H\(1\|3\|2\)>.*</H\1>'
$ <H1>heading</H1>

and

$ echo '<H1>heading</H3>' | grep '<H[1-3]>.*</H\1>'
$ grep: Invalid back reference

first command works just fine.

  1. Doesn't [1-3] and \(1\|3\|2\) both mean 1 or 2 or 3 ? if not why? and what the difference between them?
  2. why back-reference works with only \(\) ?
5
  • () means «groupping» which means set a part of the string which can operate as 1 item, and for backrefference too.
  • [] means symbols set

So if you use parenthesis just for single symbols the meaning is same. But usually it used for multisymbol strings like (cat|dog)

8

Re part 1: Yes, both patterns will match a 1 or a 2 or a 3.

Part of the answer to your first question is your second question. Funnily enough.

Parentheses are used to enable backreferences, and to clarify operator priority/grouping.

Square brackets enclose a character class. They are used to match one instance of the character set inside.

For example, \(hello\) would match the word "hello" and would also put it in the appropriate backreference variable (\1, \2, etc.)

On the other hand, [hello] would match a SINGLE character, one of the set {e,h,l,o}.

Some additional data (just extra info):

Backreferences are based on the sequence of the open paren, not the close paren. With nested parens this can become important. Ignoring escape characters for readability—(this (that) (the other)) will result in \1 containing this that the other; \2 containing that and \3 containing the other.

A count on a character class checks for more instances of that character class. They don't have to match in the same way. For instance, [0-9] will match a single digit, and [0-9]{5} will match any five digits. If you want to only match 5 repeated digits, e.g. match 77777 or 33333 but not 37497, use a backreference: \([0-9]\)\1{4}

  • Note that I've never quite gotten the hang of which special characters need to be backslashed to work in grep—I think {} may actually need backslashes to work properly and define a count. – Wildcard Oct 16 '15 at 13:08
  • The shell that you're using can affect usage of backslashes as well. – Josh Oct 16 '15 at 15:32
  • 2
    The two equivalent expressions would be '(1\|3\|2)' and '([1-3])'. The brackets replace the OR expression, not the parentheses. – TREE Oct 16 '15 at 19:31
  • 1
    well who would know better about wildcards other than wildcard? – Edward Torvalds Oct 20 '15 at 9:06
  • 1
    @edwardtorvalds, if you're trying to match only 4 repeated digits, change the {4} to a {3}. There is one digit matched by the character class in parentheses, plus the digits matched by the backreference and count (with the count specified within curly braces). The line I specified is to match 5 repeated digits and does work for that. – Wildcard Oct 20 '15 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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