Re: Regular expression help: unable to search ' # ' character inthe file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Lundh

    Re: Regular expression help: unable to search ' # ' character inthe file

    dudeja.rajat@gm ail.com wrote:
    import re
    >
    fd = open(file, 'r')
    line = fd.readline
    pat1 = re.compile("\#* ")
    while(line):
    mat1 = pat1.search(lin e)
    if mat1:
    print line
    line = fd.readline()
    I strongly doubt that this is the code you used.
    But the above prints the whole file instead of the hash lines only.
    "*" means zero or more matches. all lines is a file contain zero or
    more # characters.

    but using a RE is overkill in this case, of course. to check for a
    character or substring, use the "in" operator:

    for line in open(file):
    if "#" in line:
    print line

    </F>

Working...