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 into the block acts as the matched string in str; and I can do something to this matched string; so: str.gsub!(re){|s| s.upcase } #=> "xxxY" But what can I do to be able to manipulate the pre-match? is it the $`? I tried str.gsub!(re){|s| $`.upcase } It wouldn''t work. Thank you for your help!
Robby Russell
2009-Jul-27 05:50 UTC
Re: after matching a regexp, how can I modify the pre-match string?
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<NiKSOsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 into the > block acts as the matched string in str; and I can do something to > this matched string; so: > str.gsub!(re){|s| > s.upcase > } > #=> "xxxY" > > But what can I do to be able to manipulate the pre-match? is it the > $`? I tried > str.gsub!(re){|s| > $`.upcase > } > It wouldn''t work. > > Thank you for your help! > > > >-- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC design // development // hosting w/Ruby on Rails http://planetargon.com/ http://robbyonrails.com/ http://twitter.com/planetargon aim: planetargon +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4068 [fax]
Nik
2009-Jul-27 06:06 UTC
Re: after matching a regexp, how can I modify the pre-match string?
Oh yes, sorry about forgetting that. original string is str = "xxxy" wanted string is str = "<xxx>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 = "<abc>y" So in a more human term, it is, find where y is, and then pointy bracket whatever precedes y. And since whatever precedes y always differs from string to string. I would very much like to match y first, and then say " bracket all that precedes it" And one more thing, I read about something called the Lookahead. with the (?=). And I could have done something like: str = "xxxy" re=/(.+)(?=y)/ str.gsub!(re){|s| s = "<" + s + ">"} #=>"<xxx>y" While it worked, but in my actual string, I don''t know how I can bring this up clearly -- the string comes from the output of antiword.exe (a program that extracts text from a word document). And in it, there contains something like \267 or \306 which the (.+) cannot match even if I used the u switch for UTF-8. And so I thought, maybe I should just match y, and then manipulate whatever precedes as I originally wanted instead of trying to match what precedes y directly. of course if you know how to match \267, I''d be so glad to learn it, too! And the thing is, I don''t even know what to call a "\267". the closest to finding a name for it, I found maybe its cousin under Backlashes in the ruby book. But as to how to match them and what if there''s something else in the future that pops up in the document that''s not of this form, how will I deal with that? It''s all very frustrating. Thank You! On Jul 27, 1:50 am, Robby Russell <ro...-/Lcn8Y7Ot69QmPsQ1CNsNQ@public.gmane.org> wrote:> 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<NiKS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 into the > > block acts as the matched string in str; and I can do something to > > this matched string; so: > > str.gsub!(re){|s| > > s.upcase > > } > > #=> "xxxY" > > > But what can I do to be able to manipulate the pre-match? is it the > > $`? I tried > > str.gsub!(re){|s| > > $`.upcase > > } > > It wouldn''t work. > > > Thank you for your help! > > -- > Robby Russell > Chief Evangelist, Partner > > PLANET ARGON, LLC > design // development // hosting w/Ruby on Rails > > http://planetargon.com/http://robbyonrails.com/http://twitter.com/planetargon > aim: planetargon > > +1 503 445 2457 > +1 877 55 ARGON [toll free] > +1 815 642 4068 [fax]
spacecow
2009-Jul-27 07:44 UTC
Re: after matching a regexp, how can I modify the pre-match string?
$` should contain whatever precedes your match On 27 July, 15:06, Nik <NiKS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh yes, sorry about forgetting that. > > original string is > str = "xxxy" > > wanted string is > str = "<xxx>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 = "<abc>y" > > So in a more human term, it is, find where y is, and then pointy > bracket whatever precedes y. > > And since whatever precedes y always differs from string to string. I > would very much like to match y first, and then say " bracket all that > precedes it" > > And one more thing, I read about something called the Lookahead. with > the (?=). And I could have done something like: > str = "xxxy" > re=/(.+)(?=y)/ > str.gsub!(re){|s| s = "<" + s + ">"} > #=>"<xxx>y" > > While it worked, but in my actual string, I don''t know how I can bring > this up clearly -- the string comes from the output of antiword.exe (a > program that extracts text from a word document). And in it, there > contains something like \267 or \306 which the (.+) cannot match even > if I used the u switch for UTF-8. And so I thought, maybe I should > just match y, and then manipulate whatever precedes as I originally > wanted instead of trying to match what precedes y directly. > > of course if you know how to match \267, I''d be so glad to learn it, > too! And the thing is, I don''t even know what to call a "\267". the > closest to finding a name for it, I found maybe its cousin under > Backlashes in the ruby book. But as to how to match them and what if > there''s something else in the future that pops up in the document > that''s not of this form, how will I deal with that? It''s all very > frustrating. > > Thank You! > > On Jul 27, 1:50 am, Robby Russell <ro...-/Lcn8Y7Ot69QmPsQ1CNsNQ@public.gmane.org> wrote: > > > 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<NiKS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 into the > > > block acts as the matched string in str; and I can do something to > > > this matched string; so: > > > str.gsub!(re){|s| > > > s.upcase > > > } > > > #=> "xxxY" > > > > But what can I do to be able to manipulate the pre-match? is it the > > > $`? I tried > > > str.gsub!(re){|s| > > > $`.upcase > > > } > > > It wouldn''t work. > > > > Thank you for your help! > > > -- > > Robby Russell > > Chief Evangelist, Partner > > > PLANET ARGON, LLC > > design // development // hosting w/Ruby on Rails > > >http://planetargon.com/http://robbyonrails.com/http://twitter.com/pla... > > aim: planetargon > > > +1 503 445 2457 > > +1 877 55 ARGON [toll free] > > +1 815 642 4068 [fax]
coreypurcell
2009-Jul-28 17:05 UTC
Re: after matching a regexp, how can I modify the pre-match string?
str = "vvvyxxx" str =~ /y/ $` => "vvv" $'' => "xxx" Like spacecow said. It''s $ Backtick
Nik
2009-Jul-29 10:10 UTC
Re: after matching a regexp, how can I modify the pre-match string?
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 be read-only Any ideas? Thanks again! On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> str = "vvvyxxx" > > str =~ /y/ > > $` > => "vvv" > > $'' > => "xxx" > > Like spacecow said. It''s $ Backtick
Colin Law
2009-Jul-29 10:18 UTC
Re: after matching a regexp, how can I modify the pre-match string?
2009/7/29 Nik <NiKSOsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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 > > $` = " <#{$`}>"new_string = " <#{$`}>y" Colin> > these variables seem to be read-only > > Any ideas? > > Thanks again! > On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> str = "vvvyxxx" >> >> str =~ /y/ >> >> $` >> => "vvv" >> >> $'' >> => "xxx" >> >> Like spacecow said. It''s $ Backtick > > >
Nik So
2009-Jul-29 10:26 UTC
Re: after matching a regexp, how can I modify the pre-match string?
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 <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/7/29 Nik <NiKSOsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > 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 > > > > $` = " <#{$`}>" > > new_string = " <#{$`}>y" > > Colin > > > > > these variables seem to be read-only > > > > Any ideas? > > > > Thanks again! > > On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> str = "vvvyxxx" > >> > >> str =~ /y/ > >> > >> $` > >> => "vvv" > >> > >> $'' > >> => "xxx" > >> > >> Like spacecow said. It''s $ Backtick > > > > > > > > >-- Nik So --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Colin Law
2009-Jul-29 16:17 UTC
Re: after matching a regexp, how can I modify the pre-match string?
2009/7/29 Nik So <niksosf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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 = "<#{$`}>y<#{$''}>" or something similar. Colin> > ? > > Best, > > On Wed, Jul 29, 2009 at 6:18 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> 2009/7/29 Nik <NiKSOsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> > >> > 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 >> > >> > $` = " <#{$`}>" >> >> new_string = " <#{$`}>y" >> >> Colin >> >> > >> > these variables seem to be read-only >> > >> > Any ideas? >> > >> > Thanks again! >> > On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> str = "vvvyxxx" >> >> >> >> str =~ /y/ >> >> >> >> $` >> >> => "vvv" >> >> >> >> $'' >> >> => "xxx" >> >> >> >> Like spacecow said. It''s $ Backtick >> > > >> > >> >> > > > > -- > Nik So > > > >
Nik So
2009-Jul-29 18:46 UTC
Re: after matching a regexp, how can I modify the pre-match string?
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 <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/7/29 Nik So <niksosf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > 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 = "<#{$`}>y<#{$''}>" > or something similar. > > Colin > > > > > ? > > > > Best, > > > > On Wed, Jul 29, 2009 at 6:18 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > >> > >> 2009/7/29 Nik <NiKSOsf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >> > > >> > 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 > >> > > >> > $` = " <#{$`}>" > >> > >> new_string = " <#{$`}>y" > >> > >> Colin > >> > >> > > >> > these variables seem to be read-only > >> > > >> > Any ideas? > >> > > >> > Thanks again! > >> > On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> str = "vvvyxxx" > >> >> > >> >> str =~ /y/ > >> >> > >> >> $` > >> >> => "vvv" > >> >> > >> >> $'' > >> >> => "xxx" > >> >> > >> >> Like spacecow said. It''s $ Backtick > >> > > > >> > > >> > >> > > > > > > > > -- > > Nik So > > > > > > > > > > >-- Nik So --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
timr
2009-Jul-30 17:26 UTC
Re: after matching a regexp, how can I modify the pre-match string?
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 reference within the replacement string. On Jul 29, 11:46 am, Nik So <niks...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > 2009/7/29 Nik So <niks...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > 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 = "<#{$`}>y<#{$''}>" > > or something similar. > > > Colin > > > > ? > > > > Best, > > > > On Wed, Jul 29, 2009 at 6:18 AM, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > > wrote: > > > >> 2009/7/29 Nik <NiKS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > >> > 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 > > > >> > $` = " <#{$`}>" > > > >> new_string = " <#{$`}>y" > > > >> Colin > > > >> > these variables seem to be read-only > > > >> > Any ideas? > > > >> > Thanks again! > > >> > On Jul 28, 1:05 pm, coreypurcell <corey.purc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> >> str = "vvvyxxx" > > > >> >> str =~ /y/ > > > >> >> $` > > >> >> => "vvv" > > > >> >> $'' > > >> >> => "xxx" > > > >> >> Like spacecow said. It''s $ Backtick > > > > -- > > > Nik So > > -- > Nik So