Avatar of pepr
pepr
 asked on

Perl vs. Python -- regular expressions

Hi,

The question http:Q_27777566.html (Should I first learn Perl or Python?) led to discussion on regular expressions in Perl an Python.  Purpose of this question is to summarize up-to-date information on the subject.  If you are expert in Python and/or Perl, please suggest/write the benchmarks and help to collect the references related to the subject.

Let the http://stuffivelearned.org/doku.php?id=programming:general:phpvspythonvsperl be the starter (but it is rather obsolete).

Thanks,
    Petr
PythonPerl

Avatar of undefined
Last Comment
HonorGod

8/22/2022 - Mon
FishMonger

I don't know Python, but I was reading over that thread and the stats link you posted and wondered if we use qr(), would the Perl regex's be faster.  I haven't run any benchmarks and don't have time right now to test it.
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pepr

ASKER
I do not use Perl these days, but qr// makes sense when the same regular expression is reused many times (e.g. in the loop).  Then it should be faster than just using  q//.  The qr in Perl should be equivalent of re.compile in Python.  It makes sense to compare the variants this way in benchmarks.
SOLUTION
wilcoxon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pepr

ASKER
@wilcoxon: Let's focus on just one or two examples to start with.  What Perl version do you use?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
wilcoxon

At home, I think all of my installs are Perl 14.2 (16.x is the latest).
pepr

ASKER
I guess that the question did not attract enough attention. This way, it is probably not usefull/important. If you do not object, I am going to ask for deletion.
wilcoxon

That's fine.  I was curious but have been very busy lately.  I'll still likely play around with it at some point.  Did you see anything that was obviously inefficient in the python scripts on the original link?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pepr

ASKER
Well, there is probably not too much Python code to be completely wrong. Byt the way of writing the loop is rather strange:
...
fh = open(logfile)
while True:
    line = fh.readline()
    if not line: break
    for r in res:
        m = r.search(line)
        if m:
            counter += 1
            break
fh.close()
...

Open in new window

I would write it as:
...
f = open(logfile)
for line in f:
    for r in res:
        m = r.search(line)
        if m:
            counter += 1
            break
f.close()
...

Open in new window

or using the with construct:
...
with open(logfile) as f:
    for line in f:
        for r in res:
            m = r.search(line)
            if m:
                counter += 1
                break
...

Open in new window

When using the same regular expression in a loop, there is not reason to use the uncompiled one. (But this is the same in any language.)
SOLUTION
HonorGod

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pepr

ASKER
@HonorGod: OK. Then, it should continue somewhere to something tangible. We should decide for some examples for testing the regular expression capability.
HonorGod

agreed.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
pepr

ASKER
I have just split the points hastily to close the question. The really Happy New Year to everyone :)
HonorGod

Thank you, and thanks for the assist, and points.

Good luck & have a Happy New Year.