Ruby matching: Difference between revisions

From wikinotes
 
(No difference)

Latest revision as of 19:51, 22 June 2021

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