Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

so i have some lines that looks like this in FORTRAN.

call const (hsno, npoi*nsnolay, 0.0)

I have been using regex and python string functions to parse this code and edit some of the variables. however lines like the one above give me a problem, as the string wont split on the parentheses.

I want it to be:

[(,hsno, npoit, * nsnolay, 0.0, )]. 

However what it does is

[(hsno,...]

I want it to split on a parentheses if it is followed by a word and a comma

Is there an easy way of doing this.

share|improve this question
5  
What's your current regex? – Blender Apr 21 '13 at 21:19
up vote 7 down vote accepted

Matched parenthesis are not a regular language. That means that they can't be recognized by regular expressions in the mathematical sense. Most programming languages add additional features to make regular expressions more powerful, but it's still a pain to do stuff like this.

I'd recommend you get a proper parser. There's one I like for Python called Ply.

share|improve this answer
1  
The Ragel state machine compiler is capable of targeting Python and is great for creating parsers from state machines. – Will Apr 21 '13 at 21:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.