473,320 Members | 2,110 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,320 software developers and data experts.

RE Help

Not specific to Python, but it will be implemented in it... how do I
compile a RE to catch everything between two know values? Here's what
I've tried (but failed) to accomplish... the knowns here are START and
END:

data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)

x.findall(data)

Sep 21 '07 #1
9 1128
data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)
This should work:

x = re.compile('START(.*)END', re.DOTALL)
Sep 21 '07 #2
On Sep 21, 2:44 pm, David <wizza...@gmail.comwrote:
data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)

This should work:

x = re.compile('START(.*)END', re.DOTALL)
You'll want to use a non-greedy match:

x = re.compile(r"START(.*?)END", re.DOTALL)

Otherwise the . will match END as well.

Sep 21 '07 #3
ch************@gmail.com wrote:
On Sep 21, 2:44 pm, David <wizza...@gmail.comwrote:
>>data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)
This should work:

x = re.compile('START(.*)END', re.DOTALL)

You'll want to use a non-greedy match:

x = re.compile(r"START(.*?)END", re.DOTALL)

Otherwise the . will match END as well.
Only if there's a later END in the string, in which case the user's
requirements will determine whether greedy matching is appropriate.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 21 '07 #4
You'll want to use a non-greedy match:
x = re.compile(r"START(.*?)END", re.DOTALL)
Otherwise the . will match END as well.
On Sep 21, 3:23 pm, Steve Holden <st...@holdenweb.comwrote:
Only if there's a later END in the string, in which case the user's
requirements will determine whether greedy matching is appropriate.

regards
Steve
There will be lots of START END combinations in the data. This is more
accurate:

sfgdfg*START*dfhdgh*END*dfdgh*START*dfhfdgh*END*df gsdh*START*sdfhfdhj*END*fdghfdj

The RE should extract the data between each couples of START and END.

Thanks!

Sep 21 '07 #5
On Fri, Sep 21, 2007 at 12:05:51PM -0700, ch************@gmail.com wrote regarding Re: RE Help:
>

x = re.compile('START(.*)END', re.DOTALL)

You'll want to use a non-greedy match:

x = re.compile(r"START(.*?)END", re.DOTALL)

Otherwise the . will match END as well.
The . will only consume END if there is another END to match later on in the string. And then it's a question of desired fuctionality. If the given string is: "abcdSTARTefgENDxyzENDhijk" do you want to match "STARTefgEND" (in which case you need a non-greedy match r".*?" )? or do you want to match "STARTefgENDxyzEND" (in which case you need a greedy match: r".*" )?

Cheers,
Cliff
Sep 21 '07 #6
On Sep 21, 3:32 pm, byte8b...@gmail.com wrote:
You'll want to use a non-greedy match:
x = re.compile(r"START(.*?)END", re.DOTALL)
Otherwise the . will match END as well.

On Sep 21, 3:23 pm, Steve Holden <st...@holdenweb.comwrote:
Only if there's a later END in the string, in which case the user's
requirements will determine whether greedy matching is appropriate.
regards
Steve

There will be lots of START END combinations in the data. This is more
accurate:

sfgdfg*START*dfhdgh*END*dfdgh*START*dfhfdgh*END*df gsdh*START*sdfhfdhj*END*fdghfdj

The RE should extract the data between each couples of START and END.

Thanks!
You'll want to use my version then. Glad to help!

Sep 21 '07 #7
On Friday 21 September 2007, by*******@gmail.com wrote:
Not specific to Python, but it will be implemented in it... how do I
compile a RE to catch everything between two know values? Here's what
I've tried (but failed) to accomplish... the knowns here are START and
END:

data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)

x.findall(data)
I'm not sure finding a variable number of occurences can be done with re. How
about

# data = the string
strings = []
for s in data.split('START')[1:]:
strings.append(s.split('END')[0])
Sep 21 '07 #8
On Sep 21, 4:09 pm, Thomas Jollans <tho...@jollans.comwrote:
On Friday 21 September 2007, byte8b...@gmail.com wrote:
Not specific to Python, but it will be implemented in it... how do I
compile a RE to catch everything between two know values? Here's what
I've tried (but failed) to accomplish... the knowns here are START and
END:
data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)
x.findall(data)

I'm not sure finding a variable number of occurences can be done with re. How
about

# data = the string
strings = []
for s in data.split('START')[1:]:
strings.append(s.split('END')[0])
use re.findall :-)

Sep 21 '07 #9
Thomas Jollans wrote:
On Friday 21 September 2007, by*******@gmail.com wrote:
>Not specific to Python, but it will be implemented in it... how do I
compile a RE to catch everything between two know values? Here's what
I've tried (but failed) to accomplish... the knowns here are START and
END:

data = "asdfasgSTARTpruyerfghdfjENDhfawrgbqfgsfgsdfg"
x = re.compile('START.END', re.DOTALL)

x.findall(data)

I'm not sure finding a variable number of occurences can be done with re. How
about

# data = the string
strings = []
for s in data.split('START')[1:]:
strings.append(s.split('END')[0])
Nice. I've noticed that since I switched from Perl to Python, I hardly
ever use regular expressions anymore. In perl, they're so easy to fire
up that they become the first tool out of the toolbox, but when you make
the barrier to access just a tiny bit higher (import re/re.compile) you
start noticing how easy it is to accomplish most of those feats without
regexes, and much more readably, too.

Of course, it should be noted that the different implementations
suggested behave differently, which could also affect the choice of
method. If you have "abcSTARTdefSTARTghiEND", your version will spit
out strings = ['def', 'ghi'], but a regex, depending on whether it is
greedy or non greedy, will either spit out ['STARTdefSTARTghiEND'] or
['STARTghiEND'].

Correction, it will spit out the first one, whether greedy or not. The
difference comes with two END tags in a row.
Cheers,
Cliff
Sep 21 '07 #10

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.