Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Substring from regexp

0 views
Skip to first unread message

Eduardo Yáñez Parareda

unread,
Jun 15, 2006, 8:59:57 AM6/15/06
to
Hi all,

how could I get a substring which match a regular expression?

I mean:

"assddHellOasddaffer".something(/^H.*O$) => HellO

Marcin Mielżyński

unread,
Jun 15, 2006, 9:29:50 AM6/15/06
to

"assddHellOasddaffer"[/H.*O/]

lopex

Tim Hoolihan

unread,
Jun 15, 2006, 9:38:43 AM6/15/06
to
"assddHellOasddaffer".match(/(H.+O)/)
puts $1

The parenthesis form groups for back-referencing. You don't want the ^
or $ in your Regexp because the pattern you're looking for isn't at the
beginning or end of a string.

Eduardo Yáñez Parareda

unread,
Jun 15, 2006, 9:43:55 AM6/15/06
to
> "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, 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?

Eduardo Yáñez Parareda

unread,
Jun 15, 2006, 9:44:53 AM6/15/06
to
> "assddHellOasddaffer".match(/(H.+O)/)
> puts $1
>
> The parenthesis form groups for back-referencing. You don't want the ^
> or $ in your Regexp because the pattern you're looking for isn't at the
> beginning or end of a string.

Yes, I realized that after reading Marcin's answer...

Robert Klemme

unread,
Jun 15, 2006, 10:04:11 AM6/15/06
to
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.

Eduardo Yáñez Parareda

unread,
Jun 15, 2006, 10:06:13 AM6/15/06
to
> You want #scan:

Yes!, I tried it before post this message but may be with wrong regexp :(

> PS: It's usually better to post new questions to new threads.

I'm sorry , better next time. Thanks a lot.

Eduardo Yáñez Parareda

unread,
Jun 15, 2006, 10:36:59 AM6/15/06
to
> "fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{.*?\}/
> => ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]

I think I'm bit tedious but why it doesn't work if I put some \n inside source string?
Is '\n' character not considered when using '.*' expression?

Marcin Mielżyński

unread,
Jun 15, 2006, 11:06:20 AM6/15/06
to

use multiline option:

/.../m

lopex

0 new messages