Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the regular expression [\w-] mean?

Tags:

regex

ruby

I checked the documentation, and cannot find what [\w-] means. Can anyone tell me what [\w-] means in Ruby?

like image 328
Adam Lee Avatar asked Feb 12 '16 02:02

Adam Lee


People also ask

What does W mean in regular expression?

\w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_] ). The uppercase counterpart \W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_] ). In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart.

What does this regular expression mean [\ s ]+$?

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r] The plus sign + is a greedy quantifier, which means one or more times. For example, expression X+ matches one or more X characters.

What does the regular expression a Z0 9 \-] mean?

In a regular expression, if you have [a-z] then it matches any lowercase letter. [0-9] matches any digit. So if you have [a-z0-9], then it matches any lowercase letter or digit.

What does ?= Mean in regular expression?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


1 Answers

The square brackets [] denote a character class. A character class will match any of the things inside it.

\w is a special class called "word characters". It is shorthand for [a-zA-Z0-9_], so it will match:

  • a-z (all lowercase letters)
  • A-Z (all uppercase letters)
  • 0-9 (all digits)
  • _ (an underscore)

The class you are asking about, [\w-], is a class consisting of \w and -. So it will match the above list, plus hyphens (-).

Exactly as written, [\w-], this regex would match a single character, as long as it's in the above list, or is a dash.

If you were to add a quantifier to the end, e.g. [\w-]* or [\w-]+, then it would match any of these strings:

fooBar9
foo-Bar9
foo-Bar-9
-foo-Bar---9abc__34ab12d

And it would partially match these:

foo,Bar9                    # match 'foo' - the ',' stops the match
-foo-Bar---9*bc__34ab12d    # match '-foo-Bar---9', the '*' stops the match
like image 143
Dan Lowe Avatar answered Sep 21 '22 17:09

Dan Lowe