473,480 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex help...pretty please?

I'm trying to develop a little script that does some string
manipulation. I have some few hundred strings that currently look like
this:

cond(a,b,c)

and I want them to look like this:

cond(c,a,b)

but it gets a little more complicated because the conds themselves may
have conds within, like the following:

cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b)) ,(a<1))

What I want to do in this case is move the last parameter to the front
and then work backwards all the way out (if you're thinking recursion
too, I'm vindicated) so that it ends up looking like this:

cond((a<1), 0, cond((a<b),c,cond((a<d), e, cond((a<f), g, h))))

futhermore, the conds may be multiplied by an expression, such as the
following:

cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float(a))

Here, all I want to do is switch the parameters of the conds without
touching the expression, like so:

cond(f,-1,1)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4)+ (float(b)*2)+float(a))

So that's the gist of my problem statement. I immediately thought that
regular expressions would provide an elegant solution. I would go
through the string by conds, stripping them & the () off, until I got
to the lowest level, then move the parameters and work backwards. That
thought process became this:
-------------------------------------CODE--------------------------------------------------------
import re

def swap(left, middle, right):
left = left.replace("(", "")
right = right.replace(")", "")
temp = left
left = right
right = temp
temp = middle
middle = right
right = temp
whole = 'cond(' + left + ',' + middle + ',' + right + ')'
return whole

def condReplacer(string):
#regex = re.compile(r'cond\(.*,.*,.+\)')
regex = re.compile(r'cond\(.*,.*,.+?\)')
if not regex.search(string):
print "whole string is: " + string
[left, middle, right] = string.split(',')
right = right.replace('\'', ' ')
string = swap(left.strip(), middle.strip(), right.strip())
print "the new string is:" + string
return string
else:
more_conds = regex.search(string)
temp_string = more_conds.group()
firstParen = temp_string.find('(')
temp_string = temp_string[firstParen:]
print "there are more conditionals!" + temp_string
condReplacer(temp_string)
def lineReader(file):
for line in file:
regex = r'cond\(.*,.*,.+\)?'
if re.search(regex,line,re.DOTALL):
condReplacer(line)

if __name__ == "__main__":
input_file = open("only_conds2.txt", 'r')
lineReader(input_file)
-------------------------------------CODE--------------------------------------------------------

I think my problem lies in my regular expression... If I use the one
commented out I do a greedy search and in my test case where I have a
conditional * an expression, I grab the expression too, like so:

INPUT:

cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float(a))
OUTPUT:
whole string is:
(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float
(a))
the new string
is:cond(f*((float(e*(2**4+(float(d*8+(float(c*4+(f loat(b*2+float
(a,-1,1)

when all I really want to do is grab the part associated with the cond.
But if I do a non-greedy search I avoid that problem but stop too early
when I have an expression like this:

INPUT:
cond(a,b,(abs(c) >= d))
OUTPUT:
whole string is: (a,b,(abs(c)
the new string is:cond((abs(c,a,b)

Can anyone help me with the regular expression? Is this even the best
approach to take? Anyone have any thoughts?

Thanks for your time!

Aug 23 '06 #1
4 1898
cond(a,b,c)
>
and I want them to look like this:

cond(c,a,b)

but it gets a little more complicated because the conds themselves may
have conds within, like the following:

cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b)) ,(a<1))
Regexps are *really* *REALLY* *bad* at arbitrarily nested
structures. really.

Sounds more like you want something like a lex/yacc sort of
solution. IIUC, pyparsing may do the trick for you. I'm not a
pyparsing wonk, but I can hold my own when it comes to crazy
regexps, and can tell you from experience that regexps are *not*
a good path to try and go down for this problem.

Many times, a regexp can be hammered into solving problems
superior solutions than employing regexps. This case is not even
one of those.

If you know the maximum depth of nesting you'll encounter, you
can do some hackish stunts to shoehorn regexps to solve the
problem. But if they are truely of arbitrary nesting-depth,
*good* *luck*! :)

-tkc


Aug 23 '06 #2
MooMaster wrote:
I'm trying to develop a little script that does some string
manipulation. I have some few hundred strings that currently look like
this:

cond(a,b,c)

and I want them to look like this:

cond(c,a,b)

but it gets a little more complicated because the conds themselves may
have conds within, like the following:

cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b)) ,(a<1))

What I want to do in this case is move the last parameter to the front
and then work backwards all the way out (if you're thinking recursion
too, I'm vindicated) so that it ends up looking like this:

cond((a<1), 0, cond((a<b),c,cond((a<d), e, cond((a<f), g, h))))

futhermore, the conds may be multiplied by an expression, such as the
following:

cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float(a))

Here, all I want to do is switch the parameters of the conds without
touching the expression, like so:

cond(f,-1,1)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4)+ (float(b)*2)+float(a))

So that's the gist of my problem statement. I immediately thought that
regular expressions would provide an elegant solution. I would go
through the string by conds, stripping them & the () off, until I got
to the lowest level, then move the parameters and work backwards. That
thought process became this:
-------------------------------------CODE--------------------------------------------------------
import re

def swap(left, middle, right):
left = left.replace("(", "")
right = right.replace(")", "")
temp = left
left = right
right = temp
temp = middle
middle = right
right = temp
whole = 'cond(' + left + ',' + middle + ',' + right + ')'
return whole

def condReplacer(string):
#regex = re.compile(r'cond\(.*,.*,.+\)')
regex = re.compile(r'cond\(.*,.*,.+?\)')
if not regex.search(string):
print "whole string is: " + string
[left, middle, right] = string.split(',')
right = right.replace('\'', ' ')
string = swap(left.strip(), middle.strip(), right.strip())
print "the new string is:" + string
return string
else:
more_conds = regex.search(string)
temp_string = more_conds.group()
firstParen = temp_string.find('(')
temp_string = temp_string[firstParen:]
print "there are more conditionals!" + temp_string
condReplacer(temp_string)
def lineReader(file):
for line in file:
regex = r'cond\(.*,.*,.+\)?'
if re.search(regex,line,re.DOTALL):
condReplacer(line)

if __name__ == "__main__":
input_file = open("only_conds2.txt", 'r')
lineReader(input_file)
-------------------------------------CODE--------------------------------------------------------

I think my problem lies in my regular expression... If I use the one
commented out I do a greedy search and in my test case where I have a
conditional * an expression, I grab the expression too, like so:

INPUT:

cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float(a))
OUTPUT:
whole string is:
(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float
(a))
the new string
is:cond(f*((float(e*(2**4+(float(d*8+(float(c*4+(f loat(b*2+float
(a,-1,1)

when all I really want to do is grab the part associated with the cond.
But if I do a non-greedy search I avoid that problem but stop too early
when I have an expression like this:

INPUT:
cond(a,b,(abs(c) >= d))
OUTPUT:
whole string is: (a,b,(abs(c)
the new string is:cond((abs(c,a,b)

Can anyone help me with the regular expression? Is this even the best
approach to take? Anyone have any thoughts?

Thanks for your time!
You're gonna want a parser for this. pyparsing or spark would suffice.
However, since it looks like your source strings are valid python you
could get some traction out of the tokenize standard library module:

from tokenize import generate_tokens
from StringIO import StringIO

s =
'cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float(a))'

for t in generate_tokens(StringIO(s).readline):
print t[1],
Prints:
cond ( - 1 , 1 , f ) * ( ( float ( e ) * ( 2 ** 4 ) ) + ( float ( d ) *
8 ) + ( float ( c ) * 4 ) + ( float ( b ) * 2 ) + float ( a ) )

Once you've got that far the rest should be easy. :)

Peace,
~Simon

http://pyparsing.wikispaces.com/
http://pages.cpsc.ucalgary.ca/~aycock/spark/
http://docs.python.org/lib/module-tokenize.html

Aug 23 '06 #3
"MooMaster" <nt*****@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
I'm trying to develop a little script that does some string
manipulation. I have some few hundred strings that currently look like
this:

cond(a,b,c)

and I want them to look like this:

cond(c,a,b)
<snip>

Pyparsing makes this a fairly tractable problem. The hardest part is
defining the valid contents of a relational and arithmetic expression, which
may be found within the arguments of your cond(a,b,c) constructs.

Not guaranteeing this 100%, but it did convert your pathologically nested
example on the first try.

-- Paul

----------
from pyparsing import *

ident = ~Literal("cond") + Word(alphas)
number = Combine(Optional("-") + Word(nums) + Optional("." + Word(nums)))

arithExpr = Forward()
funcCall = ident+"("+delimitedList(arithExpr)+")"
operand = number | funcCall | ident
binop = oneOf("+ - * /")
arithExpr << ( ( operand + ZeroOrMore( binop + operand ) ) | ("(" +
arithExpr + ")" ) )
relop = oneOf("< == <= >= != <>")

condDef = Forward()
simpleCondExpr = arithExpr + ZeroOrMore( relop + arithExpr ) | condDef
multCondExpr = simpleCondExpr + "*" + arithExpr
condExpr = Forward()
condExpr << ( simpleCondExpr | multCondExpr | "(" + condExpr + ")" )

def reorderArgs(t):
return "cond(" + ",".join(["".join(t.arg3), "".join(t.arg1),
"".join(t.arg2)]) + ")"

condDef << ( Literal("cond") + "(" + Group(condExpr).setResultsName("arg1")
+ "," +
Group(condExpr).setResultsName("arg2")
+ "," +
Group(condExpr).setResultsName("arg3")
+ ")" ).setParseAction( reorderArgs )

tests = [
"cond(a,b,c)",
"cond(1>2,b,c)",
"cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+floa
t(a))",
"cond(a,b,(abs(c) >= d))",
"cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b) ),(a<1))",
]

for t in tests:
print t,"->",condExpr.transformString(t)
----------
Prints:
cond(a,b,c) -cond(c,a,b)
cond(1>2,b,c) -cond(c,1>2,b)
cond(-1,1,f)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4 )+(float(b)*2)+float
(a)) ->
cond(f,-1,1)*((float(e)*(2**4))+(float(d)*8)+(float(c)*4)+ (float(b)*2)+float
(a))
cond(a,b,(abs(c) >= d)) -cond((abs(c)>=d),a,b)
cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b)) ,(a<1)) ->
cond((a<1),0,cond((a<b),c,cond((a<d),e,cond((a<f), g,h))))
Aug 24 '06 #4
MooMaster Wrote:
I'm trying to develop a little script that does some string
manipulation. I have some few hundred strings that currently look like
this:
cond(a,b,c)
and I want them to look like this:
cond(c,a,b)
I zoned out on your question and created a very simple flipper.
Although it will not solve your problem maybe someone looking for a
simpler version may find it useful as a starting point. I hope it
proves useful. I'll post my simple flipper here:

s = 'cond(1,savv(grave(3,2,1),y,x),maxx(c,b,a),0)'
def argFlipper(s):
''' take a string of arguments and reverse'em e.g.
>>cond(1,savv(grave(3,2,1),y,x),maxx(c,b,a),0)
-cond(0,maxx(a,b,c),savv(x,y,grave(1,2,3)),1)

'''

count = 0
keyholder = {}
while 1:
if s.find('(') 0:
count += 1
value = '%sph' + '%d' % count
tempstring = [x for x in s]
startindex = s.rfind('(')
limitindex = s.find(')', startindex)
argtarget = s[startindex + 1:limitindex].split(',')
argreversed = ','.join(reversed(argtarget))
keyholder[value] = '(' + argreversed + ')'
tempstring[startindex:limitindex + 1] = value
s = ''.join(tempstring)
else:
while count and keyholder:
s = s.replace(value, keyholder[value])
count -= 1
value = '%sph' + '%d' % count
return s

print argFlipper(s)

Aug 24 '06 #5

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

Similar topics

4
9696
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
1891
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
6
4772
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
4
2647
by: Friday | last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of dot.net (and all things Windows, for that matter). As part of an experiment (to learn enough ASP/VB.net to port a series of ...
17
3938
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
9
2065
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
4
3648
by: .... | last post by:
Hi I'm trying to do something which I think is pretty simple, but really I don't know what I'm doing :-) I have a string like this HELLO WORLD 25 ERRORS
7
2561
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
4858
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a...
0
7041
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
7043
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
7081
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
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.