473,320 Members | 2,180 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.

Help with regexp please

Hi,
I'm not a regexp expert and had a bit of trouble with the following
search.

I have an "outline number" system like

1
1.2
1.2.3
1.3
2
3
3.1

etc.

I want to parse an outline number and return the parent.

So for example...

parent("1.2.3.4") returns "1.2.3"

The only way I can figure is to do two searches feeding the output of
the first into the input of the second.

Here is the code fragment...

m = re.compile(r'(\d+\.)+').match("1.2.3.4")
n = re.compile(r'\d+(\.\d+)+').match(m.string[m.start():m.end()])
parentoutlinenumber = n.string[n.start():n.end()]

parentoutlinenumber
1.2.3

How do I get that into one regexp?

Thanks for any help...

Felix
Jul 22 '05 #1
5 1202
Felix Collins wrote:
Hi,
I'm not a regexp expert and had a bit of trouble with the following search.
I have an "outline number" system like
1
1.2
1.2.3
I want to parse an outline number and return the parent.


Seems to me regex is not the way to go:
def parent(string):
return string[: string.rindex('.')]
Jul 22 '05 #2
Scott David Daniels wrote:
Felix Collins wrote:
I have an "outline number" system like
1
1.2
1.2.3
I want to parse an outline number and return the parent.


Seems to me regex is not the way to go:
def parent(string):
return string[: string.rindex('.')]


Absolutely, regex is the wrong solution for this problem. I'd suggest
using rsplit, though, since that will Do The Right Thing when a
top-level outline number is passed:
def parent(string):
return string.rsplit('.',1)[0]

Your solution will throw an exception, which may or may not be the right
behaviour.
Jul 22 '05 #3
Christopher Subich wrote:
Scott David Daniels wrote: Thanks to you both. Wow! what a quick response!
string.rsplit('.',1)[0]


Clever Python! ;-)
Sorry, I mainly code in C so I'm not very Pythonic in my thinking.
Thanks again...

Felix
Jul 22 '05 #4
On Thursday 21 July 2005 11:39 pm, Felix Collins wrote:
Christopher Subich wrote:
Scott David Daniels wrote:

Thanks to you both. Wow! what a quick response!
>string.rsplit('.',1)[0]

Clever Python! ;-)
Sorry, I mainly code in C so I'm not very Pythonic in my thinking.
Thanks again...


I think this is the "regexes can't count" problem. When the repetition
count matters, you usually need something else. Usually some
combination of string and list methods will do the trick, as here.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 22 '05 #5
Terry Hancock wrote:
I think this is the "regexes can't count" problem. When the repetition
count matters, you usually need something else. Usually some
combination of string and list methods will do the trick, as here.


Not exactly, regexes are just fine at doing things like "first" and
"last." The "regexes can't count" saying applies mostly to activities
that reduce to parentheses matching at arbitrary nesting.

The OP's problem could easily be written as a regex substitution, it's
just that there's no need to; I believe that the sub would be
(completely untested, and I'm probably going to use the wrong call to
re.sub anyway since I don't have the docs open):

re.sub(outline_value,'([0-9.]+)\.[0-9]+','\1')

It's just that the string.rsplit call is much more legible, much more
intutitive, doesn't do strange things if it's accidentally called on a
top-level outline value, and also extends immediately to handle
outlines of the form I.1.a.i.

Jul 22 '05 #6

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

Similar topics

10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
4
by: Joseph | last post by:
The idea is to show only one of the <Baby_Div> while hiding all the others. At the moment all I have managed to do is to show each <Baby_Div> in turn as expected, but the problem is that once a...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
7
by: VUNETdotUS | last post by:
How can I get the text after matching string is found: var str = "1111>AAAA<2222>BBBB<3333>CCCC"; if(str.indexOf("2222>")){ //how to get "BBBB" value following my "2222>" but before "<3333"...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
8
by: Tim Nash (aka TMN) | last post by:
Hi Can anyone help me match this div below - my regex does not work - if you could tell me why I would appreciate it. var aStr = "<div class='feedflare'>dfgdg dg</div>"; var reg = new...
4
by: Andrew Poulos | last post by:
I have a string that looks like this "cmi.interactions.fred.id" I need to test that: - the first part of the string is "cmi.interactions." (case sensitive). - the last part of the string is...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.