0

I am attempting to become better acquainted with Ruby Regex and I came accros this in one of my codewars.com answers.

My current comprehension of this Regex is as follows. the \W means any non-word character. I am assuming that the pipe symbol means something to the effect of between every character and I think that the underscore means all underscores between charecters.

Am I even close? All criticism is welcomed. Thank you.

/\W|_/
Alex Legault
  • 45
  • 1
  • 12
Erik Aasland
  • 167
  • 1
  • 11
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – nhahtdh Jul 24 '15 at 03:03

2 Answers2

6

In the regex: /\W|_/, the vertical bar is just an "or". In Ruby, _ is a word character (e.g., abc_def is a valid word in Ruby), and is not matched with the \W. So the regex above matches a non-word character OR an underscore. You can test it out at rubular.com.

lurker
  • 53,004
  • 8
  • 60
  • 93
  • Good explanation, though I don't know why you say "Normally" and "considered". As you know, the underscore is not a special character that needs to be escaped and, by "word character", you mean `'_'` matches `\w`. – Cary Swoveland Jul 24 '15 at 00:24
  • @CarySwoveland I was thinking "outside of the Ruby box" a bit, not knowing the OP's programming background. I revised my wording for clarity. – lurker Jul 24 '15 at 00:25
2

Yes, you're essentially correct. The regular expression token \W will match any non-word character. The pipe symbol — vertical bar is the alternation operator which tells the regular expression engine to match either everything to the left of the alternation operator, or everything to the right which in this case you're matching either non-word characters or the literal character _.

You could make use of a character class here instead of using alternation:

/[\W_]/
hwnd
  • 65,661
  • 4
  • 77
  • 114