下面是一个在if和then之间查找和拆分文本的示例
结果是单个元素的列表:变量、方括号和比较运算符。在code = """
If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and
EntCondL
then begin
Buy("EnStop-L") NShares shares next bar at EntPrL stop;
end;
"""
import re
words = re.split("\s+|(\(|\)||=|;)", code)
is_if = False
results = []
current = None
for token in words:
if not token:
continue
elif token.lower() == "if":
is_if = True
current = []
elif token.lower() == "then":
is_if = False
results.append(current)
elif is_if:
if token.isdecimal(): # Detect numbers
try:
current.append(int(token))
except ValueError:
current.append(float(token))
else: # otherwise just take the string
current.append(token)
print(results)
结果是:
^{pr2}$
我觉得从这里走比较容易
(我不需要以哪种形式提供数据,例如括号是否重要?)在