472,378 Members | 1,498 Online

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)
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')
-------------------------------------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?

Aug 23 '06 #1
4 1816 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)
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')
-------------------------------------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?

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))'

print t,
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
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 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 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 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 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 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 by: jmchadha | last post by: I have got the following html: "something in html ... etc.. city1... etc... info ... some html. city1.. can repeat... 4 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 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 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 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++... 0 by: BLUEPANDA | last post by: At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also... 1 by: Johno34 | last post by: I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If... 1 by: ezappsrUS | last post by: Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the... 0 by: jack2019x | last post by: hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9. 0 by: DizelArs | last post by: Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {... 0 by: F22F35 | last post by: I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...