简体   繁体   English

php 正则表达式从模式中获取所有 int 或 decimal

[英]php regex to get all int or decimal from pattern

Purpose of my code to get all take profit which has int or decimal value.我的代码的目的是让所有具有 int 或 decimal 值的获利。 writing pattern of Takeprofit will not same.止盈的书写模式会不一样。

Problem:问题:

  1. i want $m[3][4] or $m[4][4] as 1.0870 but i got only 0870. i am getting this result when number starts from 1.xxxx.我想要 $m[3][4] 或 $m[4][4] 作为 1.0870,但我只得到 0870。当数字从 1.xxxx 开始时,我得到了这个结果。 They are conflicting.他们是矛盾的。 I can not solve我无法解决
  2. TP-----1.0870 and TP=1.0870 are not detected TP-----1.0870 和 TP=1.0870 未检测到

My Code:我的代码:

<?php
$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-----1.0870 TP=1.0870 TP1=1.0870 TP Open';

$p = '#\b(TP1|TP 1|TP2|TP 2|TP3|TP 3|TAKE PROFIT 1|TAKE PROFIT 2|TAKE PROFIT 3|TAKEPROFIT 1|TAKEPROFIT 2|TAKEPROFIT 3|TAKEPROFIT\|TP)(.*?)(\bOpen\b|\b(\d+(?:\.\d+)?)\b)\b#i';

preg_match_all($p , $s , $m);

Result of $m: $m 的结果:

Array
(
    [0] => Array
        (
            [0] => TP1 1.0870
            [1] => TP 1 1.0870
            [2] => TP 2 1.0870
            [3] => Take profit 1 1.0870
            [4] => TP 1.0870
            [5] => TP1=1.0870
        )

    [1] => Array
        (
            [0] => TP1
            [1] => TP 1
            [2] => TP 2
            [3] => Take profit 1
            [4] => TP 1
            [5] => TP1
        )

    [2] => Array
        (
            [0] =>  
            [1] =>  
            [2] =>  
            [3] =>  
            [4] => .
            [5] => =
        )

    [3] => Array
        (
            [0] => 1.0870
            [1] => 1.0870
            [2] => 1.0870
            [3] => 1.0870
            [4] => 0870
            [5] => 1.0870
        )

    [4] => Array
        (
            [0] => 1.0870
            [1] => 1.0870
            [2] => 1.0870
            [3] => 1.0870
            [4] => 0870
            [5] => 1.0870
        )

)

You may use您可以使用

'~\b(TAKE ?PROFIT ?(?:[1-3]|\|TP)|TP ?(?:[1-3](?!\.\d))?)\b(.*?)\b(Open|(\d+(?:\.\d+)?))\b~i'

See the regex demo查看正则表达式演示

Details细节

  • \b - word boundary \b - 单词边界
  • (TAKE?PROFIT?(?:[1-3]|\|TP)|TP?(?:[1-3](?.\?\d))?) - Group 1 : TAKE , an optional space, PROFIT , an optional space, then a digit from 1 to 3 or |TP substring, or TP with an optional space after it that is optionally followed with 1 , 2 or 3 that are not followed with . (TAKE?PROFIT?(?:[1-3]|\|TP)|TP?(?:[1-3](?.\?\d))?) -第 1 组TAKE ,可选空格, PROFIT ,一个可选空格,然后是一个从13的数字或|TP substring,或TP后面有一个可选空格,后面可选跟123 ,后面不跟. and a digit和一个数字
  • \b - word boundary \b - 单词边界
  • (.*?) - Group 2 : any 0+ chars other than line break chars as few as possible (.*?) -第 2 组:除换行符之外的任何 0+ 字符尽可能少
  • \b - word boundary \b - 单词边界
  • (Open|(\d+(?:\.\d+)?)) - Group 3 : Open or Group 4 : 1+ digits followed with an optional sequence of . (Open|(\d+(?:\.\d+)?)) - Group 3 : OpenGroup 4 : 1+ 数字后跟可选的. and 1+ digits和 1+ 位数
  • \b - word boundary. \b - 单词边界。

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

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