startswith() and endswith() for re's would be more intuitive

M

metaperl

I just finished answering a question in #python because someone tried
to match using ... well.. match()
but did not realize that match() is actually startswith() for regexps.

I suggest:
re.compile('blah').atstartof(string)
re.compile('blah').atendof(string)

But it will never happen.
 
B

Bruno Desthuilliers

metaperl said:
I just finished answering a question in #python because someone tried
to match using ... well.. match()
but did not realize that match() is actually startswith() for regexps.

Yet someone else that failed to read the Fine Manual(tm).
I suggest:
re.compile('blah').atstartof(string)
re.compile('blah').atendof(string)

What's wrong with:
re.search(r'^blah', somestring)
re.search(r'blah$', somestring)
 
J

John Salerno

Bruno said:
Yet someone else that failed to read the Fine Manual(tm).


What's wrong with:
re.search(r'^blah', somestring)
re.search(r'blah$', somestring)

I think, since he even said himself that "match() is actually
startswith() for regexps", that he isn't complaining about the lack of
this feature, just that he wants a name change. Otherwise I'm not sure
what he wants.
 
M

metaperl

Bruno said:
Yet someone else that failed to read the Fine Manual(tm).


What's wrong with:
re.search(r'^blah', somestring)
re.search(r'blah$', somestring)

Nothing. There is also nothing wrong with

re.match('blah', somestring)

but it does read as well as

re.atstartof('blah', something)

and the counterpart for EOS is not there.
 
F

Fredrik Lundh

metaperl said:
Nothing. There is also nothing wrong with

re.match('blah', somestring)

but it does read as well as

re.atstartof('blah', something)
yuck.

and the counterpart for EOS is not there.

sure is; it's spelled:

re.match('.*blah$', somestring)

</F>
 
L

Lawrence D'Oliveiro

metaperl said:
There is also nothing wrong with

re.match('blah', somestring)

but it does read as well as

re.atstartof('blah', something)

and the counterpart for EOS is not there.

The only reason for those special cases for simple string matching is
precisely because string matching is so simple and so limited. Whereas
regular expressions give you so much more power--you can match at the
start, at the end, somewhere in the middle, 3 characters from the end, and
myriads of other possibilities. All of these can be succinctly expressed in
the RE notation itself, so there's no need for lots of special-case calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top