Prev: looking for Hamming+BCH+Reed - Solomon Codes in perl
Next: FAQ 4.52 How do I sort an array by (anything)?
From: Mr P on 4 Aug 2010 09:02 Hey Perlistas... I wanted to replace any and all multiple sequential EOLs with a single one. This worked nicely: (a) s/(\n)\n+/$1/g; But thinking abot it some more. it occurred to me that these wouldn't quite work (b) s/(\n)\n/$1/g; (c) s/\n(\n)/$1/g; (d) s/\n\n/\n/g; because even though they are greedy, they don't retrace. And predictably they didn't work. I've never really used assertions, at least not enough to be familiar with them. I guess this would be a positive look-behind to force the engine to retrace? Can someone offer an example please where I can use something like s/ (\n)\n/$1/g; with an assertion to do what (a) does? It would be instructional for me to see this example. Thanks
From: Wolf Behrenhoff on 4 Aug 2010 09:40 On 04.08.2010 15:02, Mr P wrote: > Hey Perlistas... > > I wanted to replace any and all multiple sequential EOLs with a single > one. This worked nicely: > > (a) s/(\n)\n+/$1/g; > > Can someone offer an example please where I can use something like s/ > (\n)\n/$1/g; with an assertion to do what (a) does? It would be > instructional for me to see this example. Are you looking for this? s/(\n)(?=\n)//g; # global remove \n if another \n follows Other than that, sometimes you also see something like this: 1 while s#\n\n#\n#; Wolf
From: Tad McClellan on 4 Aug 2010 09:59 Mr P <misterperl(a)gmail.com> wrote: > I wanted to replace any and all multiple sequential EOLs with a single > one. This worked nicely: > > (a) s/(\n)\n+/$1/g; You don't need regular expressions to do that: tr/\n/\n/s; or tr/\n//s; > But thinking abot it some more. it occurred to me that these wouldn't > quite work > > (b) s/(\n)\n/$1/g; > (c) s/\n(\n)/$1/g; > (d) s/\n\n/\n/g; > > because even though they are greedy, Greediness applies only when you have quantifiers. There is no "greedy" involved with any of those, as none of them have quantifiers. So greed is not why they are not working... > they don't retrace. What does "retrace" mean when you say it? > I've never really used assertions, at least not enough to be familiar > with them. I guess this would be a positive look-behind to force the > engine to retrace? I'm guessing that "retrace" means "do the search over again starting from the beginning" or some such? In other words, reset the match pos()ition? pos=0 while s/\n\n/\n/g; But since starting a match over again resets the match position to zero anyway, just this will also do it: 1 while s/\n\n/\n/g; > Can someone offer an example please where I can use something like s/ > (\n)\n/$1/g; with an assertion to do what (a) does? I don't see how assertions can help with this problem... -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: Justin C on 4 Aug 2010 09:38 On 2010-08-04, Mr P <misterperl(a)gmail.com> wrote: > Hey Perlistas... > > I wanted to replace any and all multiple sequential EOLs with a single > one. This worked nicely: > > (a) s/(\n)\n+/$1/g; > > But thinking abot it some more. it occurred to me that these wouldn't > quite work > > (b) s/(\n)\n/$1/g; > (c) s/\n(\n)/$1/g; > (d) s/\n\n/\n/g; > > because even though they are greedy, they don't retrace. And > predictably they didn't work. > > I've never really used assertions, at least not enough to be familiar > with them. I guess this would be a positive look-behind to force the > engine to retrace? > > Can someone offer an example please where I can use something like s/ > (\n)\n/$1/g; with an assertion to do what (a) does? It would be > instructional for me to see this example. I would use s/(\n){2,}/\n/g that's \n two or more times. Justin. -- Justin C, by the sea.
From: Uri Guttman on 4 Aug 2010 10:12 >>>>> "JC" == Justin C <justin.1007(a)purestblue.com> writes: JC> I would use s/(\n){2,}/\n/g that's \n two or more times. no need for the () there as you don't use the grab. and tad's tr/// is the best solution anyhow. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
Next
|
Last
Pages: 1 2 3 Prev: looking for Hamming+BCH+Reed - Solomon Codes in perl Next: FAQ 4.52 How do I sort an array by (anything)? |