Erik Aasland
Erik Aasland

Reputation: 167

Need understanding this Ruby Regex code

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|_/

Upvotes: 0

Views: 69

Answers (2)

hwnd
hwnd

Reputation: 70722

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_]/

Upvotes: 2

lurker
lurker

Reputation: 58224

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.

Upvotes: 6

Related Questions