Re: Regular Expressions Quick Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajanikanth Jammalamadaka

    Re: Regular Expressions Quick Question

    hi!

    Try this:
    >>lis=['t','tes','test ','testing']
    >>[elem for elem in lis if re.compile("^te ").search(e lem)]
    ['tes', 'test', 'testing']

    Cheers,

    Raj

    On Wed, Jul 9, 2008 at 12:13 AM, Lamonte Harris <pyth0nc0d3r@gm ail.comwrote:
    Alright, basically I have a list of words in a file and I load each word
    from each line into the array. Then basically the question is how do I
    check if the input word matches multiple words in the list.
    >
    Say someone input "test", how could I check if that word matches these list
    of words:
    >
    test
    testing
    tested
    >
    Out of the list of
    >
    Hello
    blah
    example
    test
    ested
    tested
    testing
    >
    I want it to loop then check if the input word I used starts any of the
    words in the list so if I typed 'tes'
    >
    Then:
    >
    test
    testing
    testing
    >
    would be appended to a new array.
    >
    I'm unsure how to do this in python.
    >
    Thanks in advanced.
    >
    --

    >


    --
    "For him who has conquered the mind, the mind is the best of friends;
    but for one who has failed to do so, his very mind will be the
    greatest enemy."

    Rajanikanth
  • Bruno Desthuilliers

    #2
    Re: Regular Expressions Quick Question

    Rajanikanth Jammalamadaka a écrit :
    (top-post corrected - Please, Rajanikanth, learn to trim&quote properly,
    and by all means avoid top-posting)
    >
    On Wed, Jul 9, 2008 at 12:13 AM, Lamonte Harris <pyth0nc0d3r@gm ail.comwrote:
    >Alright, basically I have a list of words in a file and I load each word
    >from each line into the array.
    <OP>
    I assume you meant 'list' ?
    </OP>
    Then basically the question is how do I
    >check if the input word matches multiple words in the list.
    >>
    >Say someone input "test", how could I check if that word matches these list
    >of words:
    >>
    >test
    >testing
    >tested
    >>
    >Out of the list of
    >>
    >Hello
    >blah
    >example
    >test
    >ested
    >tested
    >testing
    >>
    >I want it to loop then check if the input word I used starts any of the
    >words in the list so if I typed 'tes'
    >>
    >Then:
    >>
    >test
    >testing
    >testing
    <OP>
    I assume you meant:
    test
    tested
    testing
    </OP>
    >would be appended to a new array.
    hi!
    >
    Try this:
    >
    >>>lis=['t','tes','test ','testing']
    >>>[elem for elem in lis if re.compile("^te ").search(e lem)]
    Using a regexp for this is total overkill. But please at least use the
    proper regexp, and use re.compile correctly:

    exp = re.compile(r'^t es')
    found = [word for word in lis if exp.match(word)]

    But you just don't need a regexp for this - str.startswith is your friend:

    words = ['Hello', 'blah', 'example', 'test', 'ested', 'tested', 'testing']
    found = [word for word in words if word.starswith( 'tes')]
    assert found == ['test', 'tested', 'testing']

    HTH

    Comment

    • Paul McGuire

      #3
      Re: Regular Expressions Quick Question

      On Jul 9, 2:24 am, "Rajanikant h Jammalamadaka" <rajanika...@gm ail.com>
      wrote:
      hi!
      >
      Try this:
      >
      >lis=['t','tes','test ','testing']
      >[elem for elem in lis if re.compile("^te ").search(e lem)]
      >
      ['tes', 'test', 'testing']
      >
      Cheers,
      >
      Raj
      >
      >
      >
      >
      >
      On Wed, Jul 9, 2008 at 12:13 AM, Lamonte Harris <pyth0nc0...@gm ail.comwrote:
      Alright, basically I have a list of words in a file and I load each word
      from each line into the array.  Then basically the question is how doI
      check if the input word matches multiple words in the list.
      >
      Say someone input "test", how could I check if that word matches these list
      of words:
      >
      test
      testing
      tested
      >
      Out of the list of
      >
      Hello
      blah
      example
      test
      ested
      tested
      testing
      >
      I want it to loop then check if the input word I used starts any of the
      words in the list so if I typed 'tes'
      >
      Then:
      >
      test
      testing
      testing
      >
      would be appended to a new array.
      >
      I'm unsure how to do this in python.
      >
      Thanks in advanced.
      >>
      --
      "For him who has conquered the mind, the mind is the best of friends;
      but for one who has failed to do so, his very mind will be the
      greatest enemy."
      >
      Rajanikanth- Hide quoted text -
      >
      - Show quoted text -
      Give the built-in string functions a try before resorting to the re
      howitzers:
      >>lis=['t','tes','test ','testing']
      >>[elem for elem in lis if elem.startswith ("te")]
      ['tes', 'test', 'testing']

      -- Paul

      Comment

      Working...