469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Looking for help with Regular Expression

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., "[" and "]") 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.

Here's my problem: The end of my string sometimes contains a cross
reference to a section in a book and the subsections are cited using
square brackets exactly like the one I'm using as the ending point in
my original regular expression.

E.g., the text string in my data looks like this: <core:emph
typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3][b]]

But my regular expression is stopping after the first "]" so after I
add the new markup the output looks like this:

<core:emph typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3]</fn:note>[b]]

So the last subsection is outside of the note tag. I want something
like this:

<core:emph typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3][b]]</fn:note>

I'm not sure how to make my capture more greedy so I've resorted to
cleaning up the data after I make the first round of replacements:

data = re.sub(r'''\[(\d*?)\]</fn:note>\[(\w)\]\]''',
'''[\1][\2]]</fn:note>''', data)

There's got to be a better way but I'm not sure what it is.

Thanks,

Greg

May 24 '06 #1
3 1157
ProvoWallis wrote:
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., "[" and "]") 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.

Here's my problem: The end of my string sometimes contains a cross
reference to a section in a book and the subsections are cited using
square brackets exactly like the one I'm using as the ending point in
my original regular expression.

E.g., the text string in my data looks like this: <core:emph
typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3][b]]

But my regular expression is stopping after the first "]" so after I
add the new markup the output looks like this:

<core:emph typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3]</fn:note>[b]]

So the last subsection is outside of the note tag. I want something
like this:

<core:emph typestyle="it">see</core:emph> discussion in
&#xa7;&#x2002;512.16[3][b]]</fn:note>

I'm not sure how to make my capture more greedy so I've resorted to
cleaning up the data after I make the first round of replacements:

data = re.sub(r'''\[(\d*?)\]</fn:note>\[(\w)\]\]''',
'''[\1][\2]]</fn:note>''', data)

There's got to be a better way but I'm not sure what it is.


I do: Pyparsing.

from pyparsing import *
crossref = Suppress("[") + Word(alphanums, exact=1) + Suppress("]")
footnote = (
Suppress("[") + SkipTo(crossref) +
ZeroOrMore(crossref) + Suppress("]")
)

footnote.parseString("[&#xa7;&#x2002;512.16[3][b]]").asList()

py> footnote.parseString("[&#xa7;&#x2002;512.16[3][b]]").asList()
['&#xa7;&#x2002;512.16', '3', 'b']

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
May 24 '06 #2
'''\[(.*?)\]'''
?-> when this char after(*, +, ?, {n}, {n,}, {n,m}), the match pattern
is not greedy

e.g.1
String: 512.16[3][b]]
Pattern:'''\[(.*)\]'''
This will match "[3][b]]"

e.g.2
String: 512.16[3][b]]
Pattern:'''\[(.*)?\]'''
This will match "[3]" and "[b]"

May 24 '06 #3
Seem to be a lot of regular expression questions lately. There is a
neat little RE demonstrator buried down in
Python24/Tools/Scripts/redemo.py, which makes it easy to experiment
with regular expressions and immediately see the effect of changes. It
would be helpful if it were mentioned in the RE documentation, although
I can understand why one might not want a language reference to deal
with informally-supported tools.

May 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Steve | last post: by
4 posts views Thread by Buddy | last post: by
5 posts views Thread by John | last post: by
1 post views Thread by Rahul | last post: by
3 posts views Thread by Zach | last post: by
14 posts views Thread by Chris | last post: by
3 posts views Thread by Mr.Steskal | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.