Ruby matching

From wikinotes
Revision as of 19:51, 22 June 2021 by Will (talk | contribs) (→‎Documentation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation

regexp docs https://docs.ruby-lang.org/en/master/Regexp.html
regexp anchor docs https://docs.ruby-lang.org/en/master/Regexp.html#class-Regexp-label-Anchors

Basics

ruby's handling of regex looks similar to awk.

if 'ruby' =~ /^ru/
  puts 'ruby starts with 'ru'
end
lang = 'ruby'
lang = %r'ruby'  # regex string
lang = /ruby/    # regex string
ruby.sub(/by$/, 'BY')   # replace first occurrence
ruby.gsub(/by$/, 'BY')  # replace all occurrences