I often use one liners to search and replace within strings:
"my string is a string".gsub(/(st.)/, 'big \1')
=> "my big string is a big string"
Note two important syntax items:
1. put parentheses around the item to be referenced later...
2. Parenthetically captured items are accessible via \1 ref
I did originally want to overwrite the existing string, but the more I think
about it, using a new string is better.
Thanks Colin!
Nik
On Wed, Jul 29, 2009 at 12:17 PM, Colin Law wrote:
>
> 2009/7/29 Nik So :
> > I see, do you mean that, instead of over-writing my existing string, I
> > should
2009/7/29 Nik So :
> I see, do you mean that, instead of over-writing my existing string, I
> should get a new string which consists of
>
> new_str = $` + $'
I thought you wanted chevrons and the y also. You can overwrite the
existing string if you want to
str = 'abcydef'
str =~ /y/
str = "<#{$`
I see, do you mean that, instead of over-writing my existing string, I
should get a new string which consists of
new_str = $` + $'
?
Best,
On Wed, Jul 29, 2009 at 6:18 AM, Colin Law wrote:
>
> 2009/7/29 Nik :
> >
> > Hey guys, thanks for your help!
> >
> > I found out about $`, $& and $', as
2009/7/29 Nik :
>
> Hey guys, thanks for your help!
>
> I found out about $`, $& and $', as well as $1 - $9. But here is still
> the problem. They do *find* the "abc" or "xxx" in front of "y"
>
> But I *cannot* act on them, I can only read them.
> I am saying, I can't do something like
>
> $` = "
Hey guys, thanks for your help!
I found out about $`, $& and $', as well as $1 - $9. But here is still
the problem. They do *find* the "abc" or "xxx" in front of "y"
But I *cannot* act on them, I can only read them.
I am saying, I can't do something like
$` = " <#{$`}>"
these variables seem to
str = "vvvyxxx"
str =~ /y/
$`
=> "vvv"
$'
=> "xxx"
Like spacecow said. It's $ Backtick
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to rubyonrai
$` should contain whatever precedes your match
On 27 July, 15:06, Nik wrote:
> Oh yes, sorry about forgetting that.
>
> original string is
> str = "xxxy"
>
> wanted string is
> str = "y"
>
> but the x's are not constant, the y is constant.
>
> so another sample string can look like
> str2 = "a
Oh yes, sorry about forgetting that.
original string is
str = "xxxy"
wanted string is
str = "y"
but the x's are not constant, the y is constant.
so another sample string can look like
str2 = "abcy"
and the wanted string from it is
str2 = "y"
So in a more human term, it is, find where y is, an
Can you elaborate on your goal? Taking "xxxy"... what do you want the
string to look like?
Robby
On Mon, Jul 27, 2009 at 6:42 AM, Nik wrote:
>
> Hello All!
>
> I have a string:
> str = "xxxy"
>
> and a regular expression:
> re=/y/
>
> I know that if I do a gsub!, the variable that gets passed in
10 matches
Mail list logo