Re: Looking for a regular expression for this...
- Original Message - From: <[EMAIL PROTECTED]> To: Sent: Friday, July 28, 2006 10:30 PM Subject: Looking for a regular expression for this... > Hi, > My string is a multi line string that contains "filename > \n" and "host \n" entries among other things. > > For example: s = """ filename X > host hostname1 > blah... > host hostname2 > blah... > filename Y > host hostname3 > """ > Given a host name, I would like to get its filename (The closest > filename reading backwards from the host line). I could read each line > until I hit the host name, but I am looking for an RE that will do job. > The answer should be "Y" for host hostname3 and "X" for either host > hostname1 or hostname2. > > Thanks in advance. > > --Malahal. > -- > http://mail.python.org/mailman/listinfo/python-list Malahal, may I make this suggestion: ## >>> import SE # Cheese Shop >>> Names_Filter = SE.SE (' "~filename .*~=(10)=: " "~host .*~==, " ') >>> print Names_Filter (s) filename X: host hostname1, host hostname2, filename Y: host hostname3, Or: Without redundant words: >>> Names_Filter = SE.SE (' "~filename .*~=(10)=: " "~host .*~==, " | >>> "filename =" "host =" ", (10)=(10)" "~, $~=(10)" ') >>> print Names_Filter (s) X: hostname1, hostname2 Y: hostname3 ## Regards Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a regular expression for this...
[EMAIL PROTECTED] wrote: > OK, I tried this one. I am actually trying to parse dhcpd.conf file. > > def get_filename(self): > p = "^[ \t]*filename[ \t]+(\S+).*?host[ \t]+%s\s*$" % self.host > pat = re.compile(p, re.MULTILINE | re.DOTALL) > mo = pat.search(self.confdata) > if mo: > return mo.group(1) > else: > return "" > > self.host is the hostname and self.confdata is the string. It actually > matches the first filename that appears before the host entry. I want > the last one that appears before the host entry. I tried '.*?' assuming > it works, but now I know why it doesn't work! > > Since I am only interested in a particular host's filename, I could > easily parse line by line. That is how it is done now, but would like to > know if there any RE that does the trick! > Instead of .*? try (?:.(?!filename))*? Now forget about it and go back to the presumably legible code that you have already :-) Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a regular expression for this...
OK, I tried this one. I am actually trying to parse dhcpd.conf file. def get_filename(self): p = "^[ \t]*filename[ \t]+(\S+).*?host[ \t]+%s\s*$" % self.host pat = re.compile(p, re.MULTILINE | re.DOTALL) mo = pat.search(self.confdata) if mo: return mo.group(1) else: return "" self.host is the hostname and self.confdata is the string. It actually matches the first filename that appears before the host entry. I want the last one that appears before the host entry. I tried '.*?' assuming it works, but now I know why it doesn't work! Since I am only interested in a particular host's filename, I could easily parse line by line. That is how it is done now, but would like to know if there any RE that does the trick! Thanks, Malahal. faulkner [EMAIL PROTECTED] wrote: > idk, most regexes look surprisingly like undergrowth. > > malahal, why don't you parse s into a dict? read each couple of lines > into a key-value pair. > > > John Machin wrote: > > [EMAIL PROTECTED] wrote: > > > Hi, > > > My string is a multi line string that contains "filename > > > \n" and "host \n" entries among other things. > > > > > > For example: s = """ filename X > > > host hostname1 > > > blah... > > > host hostname2 > > > blah... > > > filename Y > > > host hostname3 > > > """ > > > Given a host name, I would like to get its filename (The closest > > > filename reading backwards from the host line). I could read each line > > > until I hit the host name, but I am looking for an RE that will do job. > > > > Looking for? REs don't lurk in the undergrowth waiting to be found. You > > will need to write one (unless some misguided person spoon-feeds you). > > What have you tried so far? > > > > > The answer should be "Y" for host hostname3 and "X" for either host > > > hostname1 or hostname2. > > > > > > Thanks in advance. > > > > > > --Malahal. > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a regular expression for this...
idk, most regexes look surprisingly like undergrowth. malahal, why don't you parse s into a dict? read each couple of lines into a key-value pair. John Machin wrote: > [EMAIL PROTECTED] wrote: > > Hi, > > My string is a multi line string that contains "filename > > \n" and "host \n" entries among other things. > > > > For example: s = """ filename X > > host hostname1 > > blah... > > host hostname2 > > blah... > > filename Y > > host hostname3 > > """ > > Given a host name, I would like to get its filename (The closest > > filename reading backwards from the host line). I could read each line > > until I hit the host name, but I am looking for an RE that will do job. > > Looking for? REs don't lurk in the undergrowth waiting to be found. You > will need to write one (unless some misguided person spoon-feeds you). > What have you tried so far? > > > The answer should be "Y" for host hostname3 and "X" for either host > > hostname1 or hostname2. > > > > Thanks in advance. > > > > --Malahal. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a regular expression for this...
[EMAIL PROTECTED] wrote: > Hi, > My string is a multi line string that contains "filename > \n" and "host \n" entries among other things. > > For example: s = """ filename X > host hostname1 > blah... > host hostname2 > blah... > filename Y > host hostname3 > """ > Given a host name, I would like to get its filename (The closest > filename reading backwards from the host line). I could read each line > until I hit the host name, but I am looking for an RE that will do job. Looking for? REs don't lurk in the undergrowth waiting to be found. You will need to write one (unless some misguided person spoon-feeds you). What have you tried so far? > The answer should be "Y" for host hostname3 and "X" for either host > hostname1 or hostname2. > > Thanks in advance. > > --Malahal. -- http://mail.python.org/mailman/listinfo/python-list
Looking for a regular expression for this...
Hi, My string is a multi line string that contains "filename \n" and "host \n" entries among other things. For example: s = """ filename X host hostname1 blah... host hostname2 blah... filename Y host hostname3 """ Given a host name, I would like to get its filename (The closest filename reading backwards from the host line). I could read each line until I hit the host name, but I am looking for an RE that will do job. The answer should be "Y" for host hostname3 and "X" for either host hostname1 or hostname2. Thanks in advance. --Malahal. -- http://mail.python.org/mailman/listinfo/python-list