ignore special characters in python regex

Discussion in 'Python' started by Astan Chee, Jul 21, 2009.

  1. Astan Chee

    Astan Chee Guest

    Hi,
    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

    Thanks for any help
    Astan Chee, Jul 21, 2009
    #1
    1. Advertising

  2. Astan Chee

    Frank Buss Guest

    Astan Chee wrote:

    > 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?


    Maybe re.escape helps?

    --
    Frank Buss,
    http://www.frank-buss.de, http://www.it4-systems.de
    Frank Buss, Jul 21, 2009
    #2
    1. Advertising

  3. Astan Chee

    John Machin Guest

    On Jul 21, 3:02 pm, Astan Chee <> wrote:
    > Hi,
    > 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?


    It would help if you were to say

    (1) what "ignore ... characters" means -- pretend they don't exist?
    (2) what are "special chararacters" -- non-alphanumeric?
    (3) what "treat them like normal strings" means
    (4) how you expect these special characters to be (a) ignored and (b)
    "treated like normal strings" /at the same time/.

    Cheers,
    John
    John Machin, Jul 21, 2009
    #3
    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:
    32,989
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,085
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,649
    rvino
    Aug 14, 2007
  4. Gabriel Genellina

    Re: ignore special characters in python regex

    Gabriel Genellina, Jul 21, 2009, in forum: Python
    Replies:
    0
    Views:
    498
    Gabriel Genellina
    Jul 21, 2009
  5. Rob Meade
    Replies:
    6
    Views:
    264
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page