简体   繁体   English

Python / Regex解析代码

[英]Python/Regex to parse code

so i have some lines that looks like this in FORTRAN. 所以我在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. 我一直在使用正则表达式和python字符串函数来解析这些代码并编辑一些变量。 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. 有没有一种简单的方法可以做到这一点。

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. 有一个我喜欢的Python叫做Ply。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM