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.
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.
···
On Mon, Apr 9, 2012 at 7:28 PM, Charlie B. <lists@ruby-forum.com> wrote:
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 Martín Iachetti
···
On Mon, Apr 9, 2012 at 14:28, Charlie B. <lists@ruby-forum.com> wrote:
"this sentence is my variable and the important information is {inside
these brackets}"
2012/4/9 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>
On Mon, Apr 9, 2012 at 7:28 PM, Charlie B. <lists@ruby-forum.com> 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}"
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
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:
"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
With match I didn't have that problem. It just puts every match on an array
Federico Martín Iachetti
···
2012/4/9 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>
2012/4/9 Iachetti Federico Martín <iachetti.federico@gmail.com>:
> 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
Careful, the * is greedy, so it will consume as many characters as it can:
With match I didn't have that problem. It just puts every match on an array
Federico Martín Iachetti
2012/4/9 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>
2012/4/9 Iachetti Federico Martín <iachetti.federico@gmail.com>:
> 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
Careful, the * is greedy, so it will consume as many characters as it can: