regex = Regexp.new(/param\s*=\s*([^\|]*)/)
regex.match(text).to_s
link = $1
link.strip!
espesially code like this:
regex = Regexp.new(/regex/)
regex.match(text).to_s
match = $1
I even tried gsub misuse, but it is not The Right Way®
match = gsub Regexp.new(/.*(regex).*/, '\1')
So given a string like this:
You want to extract just
"pancakes", right? If so, then:The
\s*will eat up any leading whitespace so half of yourstrip!is not needed. If you don’t want any whitespace inside the extracted value at all then:If you just want to strip off the trailing whitespace, then add an
rstrip:This one will throw an exception if
sdoesn’t your regular expression though.See
String#[]for further details.I’ve also changed your
[]*to[]+to avoid matching nothing at all. Also, you don’t have to escape most metacharacters inside a character class (see Tim’s comment) so just|is fine inside a character class.