Re: ignore special characters in python regex

Discussion in 'Python' started by Gabriel Genellina, Jul 21, 2009.

  1. En Tue, 21 Jul 2009 02:02:57 -0300, Astan Chee <>
    escribió:

    > I'm reading text from a file (per line) and I want to do a regex using
    > these lines but I want the regex to ignore any special characters and
    > treat them like normal strings.
    > Is there a regex function that can do this?
    > Here is what I have so far:
    > fp = open('file.txt','r')
    > notes = fp.readlines()
    > fp.close()
    > strin = "this is what I want"
    > for note in notes:
    > if re.search(r""+ str(note) + "",strin):
    > print "Found " + str(note) + " in " + strin


    You don't even need a regex for that.

    py> "fragil" in "supercalifragilisticexpialidocious"
    True

    Note that: r""+ str(note) + ""
    is the same as: str(note)
    which in turn is the same as: note

    Remember that each line keeps its '\n' final!

    for note in notes:
    if note.rstrip('\n') in strin:
    print "Found %s in %s" % (note, strin)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jul 21, 2009
    #1
    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. Stefan Mueller
    Replies:
    3
    Views:
    33,310
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,140
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,720
    rvino
    Aug 14, 2007
  4. Astan Chee
    Replies:
    2
    Views:
    1,022
    John Machin
    Jul 21, 2009
  5. Rob Meade
    Replies:
    6
    Views:
    297
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page