python - Building custom configuration parser to read method chaining style of configuration string
问题描述
I am building a configuration parser where using regex I will read a string configuration in the form of
- method chaining and
- method calls as parameters inside another methods.
code that will read the configuration and prints them:-
r1 = re.findall(r".\b(start|abc|def|mnp|xyz|okm)\((.*?)\)", string_expr)
for pair in r1:
operator_name = pair[0]
operator_param = pair[1]
print(operator_name,'',operator_param)
The below string_expr works fine for the regex, So I get the desired output.
string_expr:-
start().abc().def(1,2).xyz(params)
output:-
abc
def 1,2
xyz params
The issue here is whenever there is any () data inside the parenethesis, then I am not getting the whole parameter.
string_expr:-start().abc().def(1,2).xyz(mnp(okm(params)))
output:-
abc
def 1,2
xyz mnp(okm(params
instead what I want is xyz mnp(okm(params))
解决方案
使用负前瞻表达式来匹配最外面的右括号。
import re
r1 = re.findall(r".\b(start|abc|def|mnp|xyz|okm)\((.*?)\)(?![^(]*\))", 'start().abc().def(1,2).xyz(mnp(okm(params)))')
for pair in r1:
operator_name = pair[0]
operator_param = pair[1]
print(operator_name,'',operator_param)
这输出:
abc
def 1,2
xyz mnp(okm(params))
推荐阅读
- bash - 使用非 root 用户在 Dockerfile 中运行 root 服务
- javascript - 添加淡入淡出/动画
- gis - 为什么地理空间数据的 AWS Athena 二进制格式与 PostGIS 不同?
- php - 即使设置了属性,SQL Server 也会返回所有值的字符串
- vue.js - Vue和pdfkit生成错误:不是构造函数
- authentication - Blazor 本地身份验证以及 Google 和 Microsoft
- ios - 在 iOS 应用中设置播放器的最佳时间
- flutter - 如何在不弄乱实际布局顺序的情况下使用 ScrollView?
- amazon-eks - 如何限制 EKS fargate 实例的内存大小
- r - 将一个变量的值赋给另一个新变量