İsmail Ceylan
İsmail Ceylan

Reputation: 103

PHP Regex matches pattern separately on the same line

I'm trying to parse a text with php regex. I wrote a pattern match with everything in line. I do not want it. It must match the target separately on the same line.

the pattern:

/\((?P<type>\w+)((,)*(?P<params>[\d\w\:]+))((,)*(?P<comment>.*))\)/u

target data string:

lorem ipsum dolor (photo,61) test (photo,62,some text) (video,63) sit amet etc

results preg_match_all with PREG_SET_ORDER flag:

array (size=1)
0 => 
    array (size=11)
      0 => string '(photo,61) test (photo,62,some text) (video,63)' (length=47)
      'type' => string 'photo' (length=5)
      1 => string 'photo' (length=5)
      2 => string ',61' (length=3)
      3 => string ',' (length=1)
      'params' => string '61' (length=2)
      4 => string '61' (length=2)
      5 => string ') test (photo,62,some text) (video,63' (length=37)
      6 => string '' (length=0)
      'comment' => string ') test (photo,62,some text) (video,63' (length=37)
      7 => string ') test (photo,62,some text) (video,63' (length=37)

If the target data is placed in separate lines and same regex pattern execution results; (I want these results but without new lines)

lorem ipsum dolor 
(photo,61) test 
(photo,62,some text) 
(video,63) sit amet etc

array (size=3)
    0 => 
        array (size=11)
          0 => string '(photo,61)' (length=10)
          'type' => string 'photo' (length=5)
          1 => string 'photo' (length=5)
          2 => string ',61' (length=3)
          3 => string ',' (length=1)
          'params' => string '61' (length=2)
          4 => string '61' (length=2)
          5 => string '' (length=0)
          6 => string '' (length=0)
          'comment' => string '' (length=0)
          7 => string '' (length=0)
      1 => 
        array (size=11)
          0 => string '(photo,62,some text)' (length=20)
          'type' => string 'photo' (length=5)
          1 => string 'photo' (length=5)
          2 => string ',62' (length=3)
          3 => string ',' (length=1)
          'params' => string '62' (length=2)
          4 => string '62' (length=2)
          5 => string ',some text' (length=10)
          6 => string ',' (length=1)
          'comment' => string 'some text' (length=9)
          7 => string 'some text' (length=9)
      2 => 
        array (size=11)
          0 => string '(video,63)' (length=10)
          'type' => string 'video' (length=5)
          1 => string 'video' (length=5)
          2 => string ',63' (length=3)
          3 => string ',' (length=1)
          'params' => string '63' (length=2)
          4 => string '63' (length=2)
          5 => string '' (length=0)
          6 => string '' (length=0)
          'comment' => string '' (length=0)
          7 => string '' (length=0)

Thanks for your help.

Upvotes: 1

Views: 217

Answers (2)

İsmail Ceylan
İsmail Ceylan

Reputation: 103

i found a solution not affected newlines (radical solution is to enclose with "~...~" not using "/.../")

~\((\w+),([\d|:]+),*(.*?)\)~u

named version;

~\((?P<type>\w+),(?P<params>[\d|:]+),*(?P<comment>.*?)\)~u

Thanks for @Uchiha pre-solution and Thanks for the hint given.

'~\(.*?\)~'

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Simply use instead

'~\(.*?\)~'

and it'll be done

Regex101 Eval

Upvotes: 1

Related Questions