#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Explain this code.


    hie all , Can someone please Explain me the code below ..!!


    import re

    KEYWORDS = ['tINT16', 'tUINT16', 'tGDT_TYPE']

    regex = '(?:{}) +(\\w+)'.format('|'.join(map(re.escape, KEYWORDS)))

    with open ('in.txt', 'r') as file_:
    print(re.findall(regex, file_.read()))
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    140
    Start by reading the following

    http://docs.python.org/release/3.2.3/library/functions.html#format

    http://docs.python.org/release/3.2.3/library/stdtypes.html#str.join

    http://docs.python.org/release/3.2.3/library/functions.html#map

    http://docs.python.org/release/3.2.3/library/re.html#regular-expression-syntax


    Just to give you an idea
    Code:
    >>> import re
    >>> mylist = ["e1", "e221", "*a", "bg]c"]
    >>> map(re.escape, mylist)
    ['e1', 'e221', '\\*a', 'bg\\]c']
    >>> 
    >>> '|'.join(map(re.escape, mylist))
    'e1|e221|\\*a|bg\\]c'
    >>>
    Also, it might be helpful to have an extract of your file

    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo