0

Fairly new to ruby. I'm trying to parse a string and assign several variables with a regex.

I've consulted the docs, Googled a bit, and pretty sure that the following should work.

@operating_system, @os_update, @os_arch = @version_line[0].match(
        /(Solaris \d+)\s+\d+\/\d+\ss\d+[sx]_u(\d+)\wos_\d+\w+\s+(\w+)$/
        )

Where @version_line = [" Oracle Solaris 10 9/10 s10x_u9wos_14a X86\n"]

But all that happens is my first variable, @operating_system is assigned Solaris 10 9/10 s10x_u9wos_14a X86

Am I trying to do it the wrong way?

2 Answers 2

1

Actually, match returns a MatchData object, which happens to have a to_s method that produces the string you see.

To get all matched capture groups as an array, use the captures method:

@operating_system, @os_update, @os_arch = @version_line[0].match(
        /(Solaris \d+)\s+\d+\/\d+\ss\d+[sx]_u(\d+)\wos_\d+\w+\s+(\w+)$/
        ).captures
Sign up to request clarification or add additional context in comments.

4 Comments

strange… String#match works for me. It's in the docs for 1.8.7 as well as 1.9.1 and 1.9.3
Brilliant! Thanks Thomas I totally missed the fact that match returned MatchData object!
You were right about @version_line = ["..."], I'll edit my question.
Yeah, I have it too. Just not on Array ;)
0

use this regex (Solaris \d+)\s+\d+/\d+\s\w+\s\w+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.