So given a string like this:
s = "blah blah param=pancakes|eggs"
You want to extract just "pancakes"
, right? If so, then:
you_want = s[/param\s*=\s*([^|]+)/, 1]
The \s*
will eat up any leading whitespace so half of your strip!
is not needed. If you don't want any whitespace inside the extracted value at all then:
you_want = s[/param\s*=\s*([^|\s]+)/, 1]
If you just want to strip off the trailing whitespace, then add an rstrip
:
you_want = s[/param\s*=\s*([^|]*)/, 1].rstrip
This one will throw an exception if s
doesn'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.
/param\s*=\s*([^|]*)/
. But I don't know Ruby, so I can't tell what exactly that code does.[
,]
,-
and^
, depending on their position).