473,668 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1785

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(se lf):
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*********@com cast.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(se lf):
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
4167
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
5149
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 testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
4
2105
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 the string www.google.com, not http://www.google.com, and I don't want the space that the first regular expression includes...
0
1019
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 problem arises when I want to find the ones that start with www, because the string has to start with www, I dont want to match the www in http://www, since I would have activated that one already when it came up as a match for the http. The things...
3
3209
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
7
3818
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
9
3353
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 an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular expressions. Let's say my regular expression is:
3
1290
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 into a small problem. I've been using this: '''\''' as my pattern. I was expecting this to be greedy but the funny thing is that it's not greedy enough in some situations.
1
4376
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 the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
0
6181
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 long set of special symbols and impossible to improve such expression. We have to create ’smart’ regular expression. Instead of write one line expression we prepare multi line text from which we shall generate our long expression. Here is a simple...
0
8374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.