views:

95

answers:

1

How can I replace this:

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

to something shorter and one gsub call?

+7  A: 

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\": \[|\]\}$|^\{|\}$/, '')
MBO
You can combine `\]\}$` and `\}$` and use this instead: `\]?\}$`.
kejadlen
Maybe, but I didn't think about what this regex matches. I just removed `/,'').gsub(/` and inserted `|` instead :-)
MBO