[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Substring from regexp

EdUarDo

6/15/2006 1:00:00 PM

Hi all,

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

I mean:

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

Marcin Mielzynski

6/15/2006 1:30:00 PM

0

Eduardo Yáñez Parareda wrote:
> Hi all,
>
> how could I get a substring which match a regular expression?
>
> I mean:
>
> "assddHellOasddaffer".something(/^H.*O$) => HellO

"assddHellOasddaffer"[/H.*O/]

lopex

Tim Hoolihan

6/15/2006 1:39:00 PM

0

"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 wrote:
> Hi all,
>
> how could I get a substring which match a regular expression?
>
> I mean:
>
> "assddHellOasddaffer".something(/^H.*O$) => HellO

EdUarDo

6/15/2006 1:44:00 PM

0

> "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

6/15/2006 1:45:00 PM

0

> "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

6/15/2006 2:04:00 PM

0

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

6/15/2006 2:06:00 PM

0

> 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

6/15/2006 2:37:00 PM

0

> "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 Mielzynski

6/15/2006 3:06:00 PM

0

Eduardo Yáñez Parareda wrote:
>> "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?

use multiline option:

/.../m

lopex