我已经无休止地尝试让正确的正则表达式为我所追求的目的而运气而没有任何运气。希望得到一些帮助。
我有一长串文字(实际上是Gravity表格中的公式)看起来与此类似:
(({Name of Field:4} * {Name of Field (with some more info and numbers):53} +
{Name of Field:4} * {Name of Field (with some more info and numbers):55} +
{Name of Field:4} * {Name of Field (with some more info and numbers):57}) * {Name of Field (with some more info and numbers):21} ) +
(({Name of Field:4} * {Name of Field (with some more info and numbers):54} +
{Name of Field:4} * {Name of Field (with some more info and numbers):56} +
{Name of Field:4} * {Name of Field (with some more info and numbers):58}) * {Name of Field (with some more info and numbers):22} )
我最终要完成的是以一种格式获取此公式,以便我可以在PHP上使用它。我的计划是使用字段ID,我可以通过API获取我需要的信息。
所以我的第一次尝试是尝试隔离字段ID,它们是:和}之间的数字(即:: 22}),同时保留公式的逻辑(+,*, - ,/等)。
理想情况下,我最终会得到这样的结果:
({4} * {54} + {4} * {56} + {4} * {58} * {22}) ....
如果我能够达到这一点,我应该能够实现我最终的目标,但是我无法以我需要的格式成功地隔离字段ID。
答案 0 :(得分:0)
\([^()\d]*\)|({)[^:]+:|\n
试试这个。参见demo.Replace with $1
。
http://regex101.com/r/hQ9xT1/20
$re = "/\\([^()\\d]*\\)|({)[^:]+:|\\n/s";
$str = "(({Name of Field:4} * {Name of Field (with some more info and numbers):53} + \n{Name of Field:4} * {Name of Field (with some more info and numbers):55} + \n{Name of Field:4} * {Name of Field (with some more info and numbers):57}) * {Name of Field (with some more info and numbers):21} ) + \n\n(({Name of Field:4} * {Name of Field (with some more info and numbers):54} + \n{Name of Field:4} * {Name of Field (with some more info and numbers):56} + \n{Name of Field:4} * {Name of Field (with some more info and numbers):58}) * {Name of Field (with some more info and numbers):22} )";
$subst = "$1";
$result = preg_replace($re, $subst, $str);