1

I want to get all take profit value from a raw text. however writing pattern is not same.

I get all values fine which I want except for 20/30/50. for that value I get only 20.

I want whole word as 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);

result of $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
  • I want to get all take profit value from a raw text , so what are those profit values? Only numerical values? What are the different formats of numerical values? Commented Feb 1, 2020 at 6:36

1 Answer 1

2

Add (?:/\d+)* to your third capture group.

https://regex101.com/r/hsQ0xD/1/

This makes the repeating non-capturing group (substring) "slash then one or more numbers" optional.

Code: (Demo)

$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]);

Output:

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',
)
Sign up to request clarification or add additional context in comments.

1 Comment

With a better understanding of your data's variability and a battery of sample strings, I'd probably try to construct a comprehensive parsing pattern using preg_match() instead of preg_match_all(). The current technique will only become harder to extend and read as requirements and values change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.