RE: Testing for the first few letters of a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edwin.Madari@VerizonWireless.com

    RE: Testing for the first few letters of a string

    use re module

    import re
    template = '^My name is alex'
    astring = 'My name is alex, and I like pie'
    if re.match(templa te, astring):
    print 'Found it'
    else: print '%s does not begin with %s' % (astring, template)

    good luck.
    Edwin

    -----Original Message-----
    From: python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org
    [mailto:python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org]
    On Behalf Of Alexnb
    Sent: Thursday, August 07, 2008 11:40 AM
    To: python-list@python.org
    Subject: Testing for the first few letters of a string



    Okay, I have a fix for this problem, but it is messy and I think there might
    be a better way. Heres an example:

    Lets say I have a string: "My name is alex"

    and I have another string "My name is alex, and I like pie".

    I want to test to see if just the "My name is alex" part is there. I don't
    care about the pie part.
    My first instinct was to just create a for loop and test for the string like
    this:

    n = 0

    for x in string1:
    if string1[n] == string2[n]
    n = n +0
    else:
    break
    and then later testing to see what n was = to and figuring out if it got
    through the whole loop. I feel like there should be an easier way to do
    this, and probably is. So Does anyone have a suggestion?
    --
    View this message in context: http://www.nabble.com/Testing-for-th...p18873375.html
    Sent from the Python - python-list mailing list archive at Nabble.com.

    --



    The information contained in this message and any attachment may be
    proprietary, confidential, and privileged or subject to the work
    product doctrine and thus protected from disclosure. If the reader
    of this message is not the intended recipient, or an employee or
    agent responsible for delivering this message to the intended
    recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited.
    If you have received this communication in error, please notify me
    immediately by replying to this message and deleting it and all
    copies and backups thereof. Thank you.


  • John Machin

    #2
    Re: Testing for the first few letters of a string

    On Aug 8, 1:53 am, Edwin.Mad...@Ve rizonWireless.c om wrote:
    use re module
    Using re.match instead of str.startswith is overkill.
    >
    import re
    template = '^My name is alex'
    The ^ is redundant.
    astring = 'My name is alex, and I like pie'
    if re.match(templa te, astring):
    print 'Found it'
    else: print '%s does not begin with %s' % (astring, template)
    >

    Comment

    • magloca

      #3
      Re: Testing for the first few letters of a string

      John Machin wrote:
      >import re
      >template = '^My name is alex'
      >
      The ^ is redundant.
      Didn't the OP want to match only at the beginning of the string?

      m.

      Comment

      Working...