I have a variable that is a long string. The variable looks like this:
“this sentence is my variable and the important information is {inside
these brackets}”
The information I need is the information inside the brackets including
the brackets themselves, as the brackets are required for when I need to
reuse this information. How do I pull out just that piece? It will
ALWAYS be in brackets and be the only brackets in that variable.
On Mon, Apr 9, 2012 at 7:28 PM, Charlie B. [email protected] wrote:
I have a variable that is a long string. The variable looks like this:
“this sentence is my variable and the important information is {inside
these brackets}”
The information I need is the information inside the brackets including
the brackets themselves, as the brackets are required for when I need to
reuse this information. How do I pull out just that piece? It will
ALWAYS be in brackets and be the only brackets in that variable.
You can try the String#[] method and a regular expression to look for
the brackets and what’s inside. For example:
s = "asdlkmlsdmflkmflk elfm lwemf le {asdfsdf sdfdsf} asdf df "
=> "asdlkmlsdmflkmflk elfm lwemf le {asdfsdf sdfdsf} asdf df "
s[/{[^}]+}/]
=> “{asdfsdf sdfdsf}”
Jesus.
Here’s a possible approach
If there should be just one match:
pry(main)> var = “this sentence is my variable and the important
information is {inside these brackets}”
=> “this sentence is my variable and the important information is
{inside
these brackets}”
pry(main)> new_var = var.match(/({.})/)[1]*
=> “{inside these brackets}”
If there could be more than one match:
pry(main)> new_var2 = var.match(/({.})/).captures*
=> ["{inside these brackets}"]
pry(main)>
new_var has only a match, while new_var2 has an array of all the
occurrencies
Hope it helped
Federico M. Iachetti
Wow … nice one
Federico M. Iachetti
2012/4/9 Jess Gabriel y Galn [email protected]
2012/4/9 Iachetti Federico M. [email protected]:
If there could be more than one match:
pry(main)> new_var2 = var.match(/({.*})/).captures
=> [“{inside these brackets}”]
pry(main)>
new_var has only a match, while new_var2 has an array of all the
occurrencies
Careful, the * is greedy, so it will consume as many characters as it
can:
s = “adf {first one} seomthing {second one} dfdsf”
=> “adf {first one} seomthing {second one} dfdsf”
s[/({.*})/]
=> “{first one} seomthing {second one}”
That’s why I used the negative character set, to match everything that
is not a closed bracket. You can also use the ? modifier:
s[/({.*?})/]
=> “{first one}”
Jesus.
sorry … my mistake
Federico M. Iachetti
2012/4/9 Iachetti Federico M. [email protected]
With match I didn’t have that problem. It just puts every match on an
array
Federico M. Iachetti
2012/4/9 Jess Gabriel y Galn [email protected]
The regular expression by Jesus works perfect. I just tested and it
works great. Thanks for the quick turn around!
“Iachetti Federico Martín” <iachetti.federico@gmai wrote in post
#1055670:
If there could be more than one match:
pry(main)> new_var2 = var.match(/({.})/).captures*
=> ["{inside these brackets}"]
pry(main)>
This is wrong in several important ways:
var = “abc {foo} def {bar}”
=> “abc {foo} def {bar}”
var.match(/({.})/).captures
SyntaxError: compile error
(irb):2: invalid regular expression; there’s no previous pattern, to
which ‘{’ would define cardinality at 2: /({.})/
from (irb):2
var.match(/({.*})/).captures
=> ["{foo} def {bar}"]
var.match(/({.*?})/).captures
=> ["{foo}"]
What I think you’re looking for is:
var.scan(/{.*?}/)
=> ["{foo}", “{bar}”]