Robert Klemme
6/15/2006 2:04:00 PM
Eduardo Yáñez Parareda wrote:
>> "assddHellOasddaffer"[/H.*O/]
>
> Thanks, I'm newbie with Ruby but with regexps too!
>
> Well, another question. Having this string
>
>
> "fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f"
>
> if I split it with /1\{.*-\}/ I get:
>
> ["fsdfadfdsf", "f"]
>
> that's right,
Sure? I'd rather guess that you want three strings.
> but I want to get ["{1dffsdfadsf-}",
> "{1fsdfsdfsdfsdfsdh-}"] that's said, the inverse of split.
> Might I change the regexp or is there any method I could use?
You want #scan:
irb(main):003:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{[^}]*\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]
irb(main):004:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{.*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]
irb(main):005:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{[^}]*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]
Kind regards
robert
PS: It's usually better to post new questions to new threads.