From: Chad Perrin Date: 2011-07-15T09:54:48+09:00 Subject: Re: Is there a more efficient way to remove data from a string? --7JfCtLOvnd9MIVvH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jul 15, 2011 at 09:37:14AM +0900, Eddie Catflap wrote: > Hi >=20 > I have a regex as follows >=20 > REG1=3DRegexp.new=3D('dodgydata') >=20 > Basically, I need to read a file in line by line, and on each line > that's a match for my regex, remove everything from the point of the > regex to the end of that line. Is there a more efficient way to do it > than what I have at the moment? I have some HUGE files to process, and > many more regex matches to capture.. :-/ I'd probably do it something like this: ARGF.each_line do |line| line.sub!(/#{reg1}.*/, '') puts line end =2E . . unless I could be sure the end of the regex in reg1 included .* after the match you want to find, in which case the second line of that would look more like this: line.sub!(reg1, '') This, of course, assumes that reg1 actually contains a regex object. If it contains a string, go back to my first example, because strings don't deal well with regex special characters. >=20 > (As you can see I'm no ruby expert!). We all start somewhere. I wouldn't exactly call myself an "expert" either. > > I'd like to be able to make this more efficient, just doesn't seem a > very Ruby way to do things.. All help gratefully accepted! Note: I have not tested for execution time of my solution. I just wrote it off the top of my head to conform to my sense of good, clean Ruby code. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --7JfCtLOvnd9MIVvH Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk4fi/QACgkQ9mn/Pj01uKVpfgCffY2rPtBUTcuZeJxRiLnrF6ue 7goAoMRxdIUx43HouKBiRleEJr+DUwYC =KAoY -----END PGP SIGNATURE----- --7JfCtLOvnd9MIVvH--