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

Discussion in 'Python' started by metaperl, Sep 28, 2006.

  1. metaperl

    metaperl Guest

    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.
     
    metaperl, Sep 28, 2006
    #1
    1. Advertising

  2. metaperl wrote:
    > 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)


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Sep 28, 2006
    #2
    1. Advertising

  3. metaperl

    John Salerno Guest

    Bruno Desthuilliers wrote:
    > metaperl wrote:
    >> 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)


    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.
     
    John Salerno, Sep 28, 2006
    #3
  4. metaperl

    metaperl Guest

    Bruno Desthuilliers wrote:
    > metaperl wrote:
    > > 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)


    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.
     
    metaperl, Sep 28, 2006
    #4
  5. metaperl wrote:

    > 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>
     
    Fredrik Lundh, Sep 28, 2006
    #5
  6. In message <>, metaperl
    wrote:

    > 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.
     
    Lawrence D'Oliveiro, Sep 29, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,934
    Chris Uppal
    Nov 16, 2005
  2. Roedy Green
    Replies:
    14
    Views:
    742
    Mickey Segal
    Jan 31, 2006
  3. Michele Simionato

    feature request: a better str.endswith

    Michele Simionato, Jul 18, 2003, in forum: Python
    Replies:
    24
    Views:
    805
    Peter Hansen
    Jan 9, 2004
  4. Batista, Facundo

    RE: Slicing vs .startswith

    Batista, Facundo, Sep 22, 2003, in forum: Python
    Replies:
    0
    Views:
    414
    Batista, Facundo
    Sep 22, 2003
  5. Ethan Furman
    Replies:
    4
    Views:
    269
    Roy Smith
    May 27, 2011
Loading...

Share This Page