regex help

G

Gabriel Rossetti

Hello everyone,

I'm going nuts with some regex, could someone please show me what I'm
doing wrong?

I have an XMPP msg :

<message xmlns='jabber:client' to='(e-mail address removed)'>
<mynode xmlns='myprotocol:core' version='1.0' type='mytype'>
<parameters>
<param1>123</param1>
<param2>456</param2>
</parameters>
<payload type='plain'>...</payload>
</mynode>
<x xmlns='jabber:x:expire' seconds='15'/>
</message>

the <parameter> node may be absent or empty (<parameter/>), the <x> node
may be absent. I'd like to grab everything exept the <payload> nod and
create something new using regex, with the XMPP message example above
I'd get this :

<message xmlns='jabber:client' to='(e-mail address removed)'>
<mynode xmlns='myprotocol:core' version='1.0' type='mytype'>
<parameters>
<param1>123</param1>
<param2>456</param2>
</parameters>
</mynode>
<x xmlns='jabber:x:expire' seconds='15'/>
</message>

for some reason my regex doesn't work correctly :

r"(<message .*?>).*?(<mynode
..*?>).*?(?:(<parameters>.*?</parameters>)|<parameters/>)?.*?(<x .*/>)?"

I group the opening <message> node, the opening <mynode> node and if the
xmlns='myprotocol:core' version='1.0'
type='mytype'><parameters><param1>123</param1><param2>456</param2></parameters><payload
xmlns='myprotocol:core' version='1.0'
type='mytype'><parameters/><payload
xmlns='myprotocol:core' version='1.0' type='mytype'><payload
xmlns='myprotocol:core' version='1.0'
xmlns='myprotocol:core' version='1.0'
xmlns='myprotocol:core' version='1.0' type='mytype'><payload
("<message xmlns='jabber:client' to='(e-mail address removed)'>", "<mynode
xmlns='myprotocol:core' version='1.0' type='mytype'>",
("<message xmlns='jabber:client' to='(e-mail address removed)'>", "<mynode
xmlns='myprotocol:core' version='1.0' type='mytype'>",


Does someone know what is wrong with my expression? Thank you, Gabriel
 
R

r0g

Gabriel said:
Hello everyone,

I'm going nuts with some regex, could someone please show me what I'm
doing wrong?

I have an XMPP msg :
Does someone know what is wrong with my expression? Thank you, Gabriel




Gabriel, trying to debug a long regex in situ can be a nightmare however
the following technique always works for me...

Use the interactive interpreter and see if half the regex works, if it
does your problem is in the second half, if not it's in the first so try
the first half of that and so on an so forth. You'll find the point at
which it goes wrong in a snip.

Non-trivial regexes are always best built up and tested a bit at a time,
the interactive interpreter is great for this.

Roger.
 
I

Intchanter / Daniel Fackrell

Gabriel, trying to debug a long regex in situ can be a nightmare however
the following technique always works for me...

Use the interactive interpreter and see if half the regex works, if it
does your problem is in the second half, if not it's in the first so try
the first half of that and so on an so forth. You'll find the point at
which it goes wrong in a snip.

Non-trivial regexes are always best built up and tested a bit at a time,
the interactive interpreter is great for this.

Roger.

I'll just add that the "now you have two problems" quip applies here,
especially when there are very good XML parsing libraries for Python
that will keep you from having to reinvent the wheel for every little
change.

See sections 20.5 through 20.13 of the Python Documentation for
several built-in options, and I'm sure there are many community
projects that may fit the bill if none of those happen to.

Personally, I consider regular expressions of any substantial length
and complexity to be bad practice as it inhibits readability and
maintainability. They are also decidedly non-Zen on at least
"Readability counts" and "Sparse is better than dense".

Intchanter
Daniel Fackrell

P.S. I'm not sure how any of these libraries are implemented yet, but
I'd hope they're using a finite state machine tailored to the parsing
task rather than using regexes, but even if they do the latter, having
that abstracted out in a mature library with a clean interface is
still a huge win.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top