Robert Lemiesz
Robert Lemiesz

Reputation: 1156

Python/Regex to parse code

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.

Upvotes: 2

Views: 244

Answers (1)

Antimony
Antimony

Reputation: 39511

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.

Upvotes: 7

Related Questions