How to pull out specific part of a variable and set to another 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.

···

--
Posted via http://www.ruby-forum.com/.

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 = "asdl­kmlsdmflkm­flk elfm lwemf­ le {asdf­sdf sdfds­f} 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:

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 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}"

The regular expression by Jesus works perfect. I just tested and it
works great. Thanks for the quick turn around!

···

--
Posted via http://www.ruby-forum.com/.

Wow ... nice one
Federico Martín Iachetti

···

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 = "asdl­kmlsdmflkm­flk elfm lwemf­ le {asdf­sdf sdfds­f} 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

Careful, the * is greedy, so it will consume as many characters as it can:

s = "adf {firs­t one} seomt­hing {seco­nd 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.

···

2012/4/9 Iachetti Federico Martín <iachetti.federico@gmail.com>:

"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}"]

···

--
Posted via http://www.ruby-forum.com/\.

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:

> s = "adf {firs­t one} seomt­hing {seco­nd 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 Martín Iachetti

···

2012/4/9 Iachetti Federico Martín <iachetti.federico@gmail.com>

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:

> s = "adf {firs­t one} seomt­hing {seco­nd 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.