Scanning for more than one specific character with String#scan

J

Jeppe Jakobsen

------=_Part_18005_11077550.1138555887731
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi all, here is my question:

Lets say that I have a variable:

aString =3D "1234s * 4234k"

I'll then like to scan for the units (the s and the k) after each integer:

anArray =3D aString.scan(/s/)

Well that got me the s, but what expression do I have to use, if I want to
scan for both s and k?

------=_Part_18005_11077550.1138555887731--
 
C

Cameron McBride

Greetings,

Lets say that I have a variable:

aString =3D "1234s * 4234k"

I'll then like to scan for the units (the s and the k) after each integer= :

anArray =3D aString.scan(/s/)

Well that got me the s, but what expression do I have to use, if I want t= o
scan for both s and k?

if you have an explicit list:
anArray =3D aString.scan(/[sk]/) #=3D> ["s", "k"]

if you just want any lowercase letter:
anArray =3D aString.scan(/[a-z]/) #=3D> ["s", "k"]

String.scan takes a regex, you might want to look into that. Have fun!

Cameron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top