Search code examples
rubyregexsimplification

Make regexp shorter


I have the following text: var avarb avar var varb var. What I want to do is to extract only "direct" var occurrences. The above string contains 3 of them.

While playing with rubular I made up the following Regexp: /\A(var)|\s(var)\s|(var)\z/. Is there a way to simplify it, in order to use var substring in regexp only once?


Solution

  • Try this one using word boundaries:

     /\bvar\b/