Looking for help with Regular Expression

P

ProvoWallis

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
§ 512.16[3]]

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
§ 512.16[3]</fn:note>]

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

<core:emph typestyle="it">see</core:emph> discussion in
§ 512.16[3]]</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
 
J

James Stroud

ProvoWallis said:
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
§ 512.16[3]]

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
§ 512.16[3]</fn:note>]

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

<core:emph typestyle="it">see</core:emph> discussion in
§ 512.16[3]]</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("[§ 512.16[3]]").asList()

py> footnote.parseString("[§ 512.16[3]]").asList()
['§ 512.16', '3', 'b']

James

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

http://www.jamesstroud.com/
 
L

lao_mage

'''\[(.*?)\]'''
?-> when this char after(*, +, ?, {n}, {n,}, {n,m}), the match pattern
is not greedy

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

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

Roger Miller

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top