From: "Iachetti Federico Martín" Date: 2012-04-10T03:05:53+09:00 Subject: Re: How to pull out specific part of a variable and set to another variable? --90e6ba53b1e051e21d04bd42db00 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable With match I didn't have that problem. It just puts every match on an array Federico Mart=EDn Iachetti 2012/4/9 Jes=FAs Gabriel y Gal=E1n > 2012/4/9 Iachetti Federico Mart=EDn : > > Here's a possible approach > > > > If there should be just one match: > > > > pry(main)> var =3D "this sentence is my variable and the important > information > > is {inside these brackets}" > > =3D> "this sentence is my variable and the important information is {in= side > > these brackets}" > > pry(main)> new_var =3D var.match(/({.*})/)[1] > > =3D> "{inside these brackets}" > > > > If there could be more than one match: > > > > pry(main)> new_var2 =3D var.match(/({.*})/).captures > > =3D> ["{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 =3D "adf {firs=ADt one} seomt=ADhing {seco=ADnd one} dfdsf=AD" > =3D> "adf {first one} seomthing {second one} dfdsf" > > s[/({.*})/=AD] > =3D> "{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[/({.*?})=AD/] > =3D> "{first one}" > > Jesus. > > --90e6ba53b1e051e21d04bd42db00 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable With match I didn't have that problem. It just puts every match on an a= rray

Federico Mart=EDn Iachetti


2012/4/9 Jes=FAs Gabriel y Gal=E1n <jgabrielygalan= @gmail.com>
2012/4/9 Iachetti Federico Mart=EDn <iachetti.federico@gmail.com>:
> Here's a possible approach
>
> If there should be just one match:
>
> pry(main)> var =3D "this sentence is my variable and the impor= tant information
> is {inside these brackets}"
> =3D> "this sentence is my variable and the important informati= on is {inside
> these brackets}"
> pry(main)> new_var =3D var.match(/({.*})/)[1]
> =3D> "{inside these brackets}"
>
> If there could be more than one match:
>
> pry(main)> new_var2 =3D var.match(/({.*})/).captures
> =3D> ["{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 =3D "adf {firs=ADt one} seomt=ADhing {seco=ADnd one} dfdsf=AD&q= uot;
=3D> "adf {first one} seomthing {second one} dfdsf"
> s[/({.*})/=AD]
=3D> "{first one} seomthing {second one}"

That's why I used the negative character set, to match everything that<= br> is not a closed bracket. You can also use the ? modifier:

> s[/({.*?})=AD/]
=3D> "{first one}"

Jesus.


--90e6ba53b1e051e21d04bd42db00--