On Dec 4, 2013, at 11:50 AM, Jes=FAs Gabriel y Gal=E1n =
<jgabrielygalan / gmail.com> wrote:

> On Wed, Dec 4, 2013 at 5:36 PM, Stuart Clarke <lists / ruby-forum.com> =
wrote:
>> Hi all,
>>=20
>> I have several strings of data, which are all very similar, however I
>> only wish to look at some strings which match a specific criteria and
>> ignore the rest. Some samples are below - I want the first and the =
last
>> string and to ignore the middle string.
>>=20
>> /software/$$$PROTO.HIV/Microsoft/Windows NT/CurrentVersion
>>=20
>> =
/software/CMI-CreateHive{199ADFC2-6E16-4946-BE90-5A3EC3A60902}/Wow6432Node=
/Microsoft/Office/12.0/User
>> Settings/Word_Core/Delete/Software/Microsoft/Windows =
NT/CurrentVersion
>>=20
>> =
/software/CMI-CreateHive{199ADFC2-6E16-4946-BE90-5A3EC3A60902}/Microsoft/W=
indows
>> NT/CurrentVersion
>>=20
>> I have constructed a regex to say capture any string starting with
>> /software/ and ending with /Microsoft/Windows NT/CurrentVersion, but
>> with only one string within slashes in the middle, see below:
>>=20
>> \/software\/(.*?)\/Microsoft\/Windows NT\/CurrentVersion$
>>=20
>> This regex captures everything because (.*?) takes everything. Any =
ideas
>> how I can achieve this? My brain is frying.
>=20
> Try this:
>=20
> strings =3D ["/software/$$$PROTO.HIV/Microsoft/Windows
> NT/CurrentVersion",
> =
"/software/CMI-CreateHive{199ADFC2-6E16-4946-BE90-5A3EC3A60902}/Wow6432Nod=
e/Microsoft/Office/12.0/User
> Settings/Word_Core/Delete/Software/Microsoft/Windows
> NT/CurrentVersion",
> =
"/software/CMI-CreateHive{199ADFC2-6E16-4946-BE90-5A3EC3A60902}/Microsoft/=
Windows
> NT/CurrentVersion"]
>=20
> re =3D /\/software\/([^\/]*)\/Microsoft\/Windows NT\/CurrentVersion$/
>=20
> 2.0.0p195 :018 > strings.each do |s|
> 2.0.0p195 :019 >     m =3D re.match(s)
> 2.0.0p195 :020?>   puts m.captures if m
> 2.0.0p195 :021?>   end
> $$$PROTO.HIV
> CMI-CreateHive{199ADFC2-6E16-4946-BE90-5A3EC3A60902}
>=20
> [^\/] is a character class that will match everything that is not a
> forward slash. This is repeated zero or more times.
>=20
> Jesus.

Remember that you can use %r to quote regular expressions if the =
presence of the forward slash causes a lot of escaping. For example in

ratdog:mcqd mike$ pry
[1] pry(main)> re =3D %r(/software/([^/]*)/Microsoft/Windows =
NT/CurrentVersion\z) =3D> /\/software\/([^\/]*)\/Microsoft\/Windows =
NT\/CurrentVersion\z/


%r(/software/([^/]*)/Microsoft/Windows NT/CurrentVersion\z)

seems more reasonable than

/\/software\/([^\/]*)\/Microsoft\/Windows NT\/CurrentVersion$/

I changed $ to \z for matching the end of string as well because it =
matches the real end of string:

[3] pry(main)> /Hello$/.match "Hello\n"
=3D> #<MatchData "Hello">
[4] pry(main)> /Hello\z/.match "Hello\n"
=3D> nil

Hope this helps,

Mike

--=20

Mike Stok <mike / stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.