我想从原始文本中获取所有止盈值。但是书写模式并不相同。我得到了我想要的所有值,除了20/30/50。对于这个值,我只得到20。我想要整个单词作为20/30/50$s = 'SS 1.0140 SL 1.0670 TP1 1.0870 TP 1 1.0870 TP 2 1.0870 Takeprofit1 1.0870 Take profit 1 1.0870 TP 1.0870 TP 20/30/50 TP-----1.0870 TP=1.0870 TP1=1.0870 TP Open';$p = '#\b(TAKE ?PROFIT ?(?:[1-3]|\|TP|at)|TP ?(?:[1-3](?!\.\d))?)\b(.*?)\b(Open|(\d+(?:\.\d+)?))\b#i';preg_match_all($p , $s , $m);结果:$m[3]Array( [3] => Array ( [0] => 1.0870 [1] => 1.0870 [2] => 1.0870 [3] => 1.0870 [4] => 1.0870 [5] => 1.0870 [6] => 20 [7] => 1.0870 [8] => 1.0870 [9] => 1.0870 [10] => Open ))
1 回答
红颜莎娜
TA贡献1842条经验 获得超12个赞
添加到第三个捕获组。(?:/\d+)*
https://regex101.com/r/hsQ0xD/1/
这使得重复的非捕获组(子字符串)“斜杠然后一个或多个数字”成为可选。
代码:(演示)
$s = 'SS 1.0140 SL 1.0670 TP1 1.0870 TP 1 1.0870 TP 2 1.0870 Takeprofit1 1.0870 Take profit 1 1.0870 TP 1.0870 TP 20/30/50 TP-----1.0870 TP=1.0870 TP1=1.0870 TP Open';
$p = '#\b(TAKE ?PROFIT ?(?:[1-3]|\|TP|at)|TP ?(?:[1-3](?!\.\d))?)\b(.*?)\b(Open|(\d+(?:\.\d+)?(?:/\d+)*))\b#i';
preg_match_all($p , $s , $m);
var_export($m[3]);
输出:
array (
0 => '1.0870',
1 => '1.0870',
2 => '1.0870',
3 => '1.0870',
4 => '1.0870',
5 => '1.0870',
6 => '20/30/50',
7 => '1.0870',
8 => '1.0870',
9 => '1.0870',
10 => 'Open',
)
- 1 回答
- 0 关注
- 70 浏览
添加回答
举报
0/150
提交
取消