Hacker News new | past | comments | ask | show | jobs | submit login

You're right— I didn't say what I really meant there. It's not the regex syntax itself, but the syntax surrounding the use of regex's which is more efficient. Compare the number of keystrokes in his examples above (e.g. $string =~ s/search pattern/replacement string/g;) with what you'd have to do using nearly any other language. In Python for example, remember that you'd need to precompile the regex to even approach 8x slower speeds.



Well, that was inspired by sed. I have to do a test in Ruby too see how fast regexos are, being spoiled by Perl usage and all. What I don't like is this:

  my $a = 'Perl';
  my $b = $a;
  $b =~ s/pe/ea/i;
Why not do something to $a and obtain $b, just like js does with Array.map. Why the need to copy $a into to $b and then replace? Much more ellegant in Ruby:

  a = 'Ruby'
  b = a.gsub(/ru/i, 'mo')


oh— for regex performance Perl blows Swift, Java, Python, Ruby, Go, and C++ out of the water. For simple string manipulation— substring replacement, string reversal, etc. it's never the worst but never the best. Scroll down to the bottom of this paper where they test actual regexes though and the difference is pretty stunning.

http://jultika.oulu.fi/files/nbnfioulu-202001201035.pdf


No need to copy and then replace:

  my $a =  'Perl';
  my $b =~ s/pe/ea/ir;


Doesn't work. You don't assign anything to $b, so it's undef:

  % perl -MData::Dumper=Dumper -e 'my $a = 'Perl'; my $b =~ s/pe/ea/ir; print Dumper $b;'
  $VAR1 = undef;
You probably mean:

  my $a = 'Perl';
  my $b = $a =~ s/pe/ea/ir;
Anyway, thank you for the /r modifier, it didn't know what it did, since there's no example in perlre(1).


You're right, that's exactly what I meant!

I'm surprised there's no example in perlre(1); perhaps I can get that corrected.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: