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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    2,195
    Chris Uppal
    Nov 16, 2005
  2. Roedy Green
    Replies:
    14
    Views:
    859
    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:
    1,150
    Peter Hansen
    Jan 9, 2004
  4. Brian Cole
    Replies:
    1
    Views:
    489
    Paul Rubin
    Jan 10, 2006
  5. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    Significance of "start" parameter to string method "endswith"

    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007, in forum: Python
    Replies:
    4
    Views:
    642
    John Machin
    Apr 19, 2007
  6. Replies:
    13
    Views:
    1,034
    Dennis Lee Bieber
    Sep 11, 2007
  7. Ethan Furman
    Replies:
    4
    Views:
    472
    Roy Smith
    May 27, 2011
  8. Roedy Green

    startsWith datastructure

    Roedy Green, Oct 27, 2012, in forum: Java
    Replies:
    2
    Views:
    330
    Robert Klemme
    Oct 27, 2012
Loading...