0

I have 2 questions regarding the following regex from Why's Poignant Guide to Ruby:

1: What does the minus sign mean here? It doesn't seem to be designating a range because there is nothing to the left of it other than the bracket.

2: Why is it necessary to escape the closing parenthesis? After you escape the opening one, what special meaning could the closing parenthesis have?

   /\([-\w]+\)/
mrwnt10
  • 1,234
  • 1
  • 17
  • 28

4 Answers4

5

1)When the minus sign is at the begining or at the end of a character class, it is seen as literal.

2) escaping closing parenthesis is a convention. The goal is IMO, to avoid an ambiguity with a possible opening parenthesis before. Consider these examples:

/(\([-\w]+\))/    or     /(\([-\w]+)\)/ 
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
3

1) The minus sign is a literal minus sign. Since it cannot possibly designate a range, it has no special meaning and so the character class is equivalent to [\-\w] - escaping the hyphen is optional, as you observe in your second point...

2) ...however, it isn't always good form to not escape something just because the regular expression engine allows it. For example, this regex: ([([^)-]+) is perfectly valid (I think...) but entirely unclear because of the fact that characters which normally have special meanings are used as literal characters without being escaped. Valid, yes, but not obvious, and someone who doesn't know all the rules will become very confused trying to understand it.

Dan
  • 10,531
  • 2
  • 36
  • 55
  • In ruby, square brackets must be escaped inside a character class. There is a fabulous topic about the subject here : http://stackoverflow.com/questions/17845014/what-does-the-regex-mean/17845034#17845034 – Casimir et Hippolyte Aug 07 '13 at 15:10
1

The the minus sign -, or say the hyphen, means exact just the character -. The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. It's not designating a range, so it's not confusing. You can also choose to use \- if you like.

As to why to escape ), I think it means to reduce the regex engine's work so that it doesn't have to remember if an opening parenthesis is before.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

- sign in this regex actually means a - sign that you want to see in the text.

Non-escaped parentheses means a match group, that will be available for you, for example, by $1 variable.

> "(-w)" =~ /\([-\w]+\)/
> $1 # => nil

and

> "(-w)" =~ /([-\w]+)/
> $1 # => -w

You can go to Rubular and try both regexes \([-\w]+\) and ([-\w]+) - and you will see different results by passing (-w) as a test. You can notice match groups appearing.

Nikita Chernov
  • 2,031
  • 16
  • 13