473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Looking for a regular expression for this...

Hi,
My string is a multi line string that contains "filename
<filename>\n" and "host <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.
Jul 28 '06 #1
4 1769

ma*****@us.ibm.com wrote:
Hi,
My string is a multi line string that contains "filename
<filename>\n" and "host <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.
Jul 28 '06 #2
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:
ma*****@us.ibm.com wrote:
Hi,
My string is a multi line string that contains "filename
<filename>\n" and "host <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.
Jul 28 '06 #3
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 [fa*********@comcast.net] 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:
ma*****@us.ibm.com wrote:
Hi,
My string is a multi line string that contains "filename
<filename>\n" and "host <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
Jul 29 '06 #4

ma*****@us.ibm.com 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

Jul 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: hooterbite | last post by:
string: www.google.com "\\swww\\S*" returns " www.google.com' string: http://www.google.com "\\dwww\\d\\S*" matches, seems like it doesn't mind the http:// before the www. I want to match...
0
by: hooterbite | last post by:
I need to loop through paragraphs of text and "activate" the links, which could either have the http:// or just start with www. I have no problem finding the ones that start with http://. The...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: ProvoWallis | last post by:
Hi, I'm looking for a little advice about regular expressions. I want to capture a string of text that falls between an opening squre bracket and a closing square bracket (e.g., "") but I've run...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.