473,396 Members | 1,891 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,396 software developers and data experts.

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 1277
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
4
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...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
5
by: John | last post by:
I am new in Regular Expression. Could someone please help me in following expression? 1. the string cannot be empty 2. the string can only contains AlphaNumeric characters. No space or any...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
6
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description:...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.