Forum: Ruby Scanning for more than one specific character with String#sc

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeppe J. (Guest)
on 2006-01-29 19:34
(Received via mailing list)
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?
Cameron McBride (Guest)
on 2006-01-29 19:49
(Received via mailing list)
Greetings,

On 1/29/06, Jeppe J. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.