[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Scanning for more than one specific character with String#scan

Jeppe Jakobsen

1/29/2006 5:32:00 PM

Hi all, here is my question:

Lets say that I have a variable:

aString = "1234s * 4234k"

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

anArray = 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?
2 Answers

Marcin Mielzynski

1/29/2006 5:38:00 PM

0

Jeppe Jakobsen wrote:

> aString = "1234s * 4234k"

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

p "1234s * 4234k".scan(/\d+[sk]/)

lopex

Cameron McBride

1/29/2006 5:47:00 PM

0

Greetings,

On 1/29/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> Lets say that I have a variable:
>
> aString = "1234s * 4234k"
>
> I'll then like to scan for the units (the s and the k) after each integer:
>
> anArray = 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?

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

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

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

Cameron