From: "Iachetti Federico Martín" Date: 2012-04-10T03:07:11+09:00 Subject: Re: How to pull out specific part of a variable and set to another variable? --20cf3071ce04eebe6804bd42df38 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable sorry ... my mistake Federico Mart=EDn Iachetti 2012/4/9 Iachetti Federico Mart=EDn > With match I didn't have that problem. It just puts every match on an arr= ay > > 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 >> {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 ca= n: >> >> > 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. >> >> > --20cf3071ce04eebe6804bd42df38 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable sorry ... my mistake
Federico Mart=EDn Iachetti


2012/4/9 Iachetti Federico Mart=EDn <iachetti.f= ederico@gmail.com>
With match I didn't have that problem. It just puts every match on an a= rray

F= ederico 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.



--20cf3071ce04eebe6804bd42df38--