Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make one gsub call instead of five

How can I replace this:

lyrics = lyrics.gsub(/\n/,'').gsub(/^\{\"similar\": \[/, '').gsub(/\]\}$/, '').gsub(/^\{/, '').gsub(/\}$/, '')

to something shorter and one gsub call?

like image 686
mroztn Avatar asked Jan 23 '23 19:01

mroztn


1 Answers

You can joint multiple regexes into one by using alternate symbol | and creating branches in regex. Pay attention to anchors like ^, $ and other, because if they appear in one branch, they only work for that branch, not whole regex

lyrics = lyrics.gsub(/\n|^\{\"similar\": \[|\]\}$|^\{|\}$/, '')
like image 129
MBO Avatar answered Jan 25 '23 10:01

MBO