how could I get a substring which match a regular expression?
I mean:
"assddHellOasddaffer".something(/^H.*O$) => HellO
"assddHellOasddaffer"[/H.*O/]
lopex
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.
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?
Yes, I realized that after reading Marcin's answer...
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.
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.
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