1

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.

11
  • 1
    I do not know what you mean Commented Jul 31, 2015 at 7:28
  • What you mean by without new lines can you please elaborate Commented Jul 31, 2015 at 7:29
  • @Uchiha For example, moving to a new line with the Enter key. Programatically: \r\n. This character is available, it works but I want to work when it is not. Commented Jul 31, 2015 at 7:36
  • Try this '~\(.*?\)~' Commented Jul 31, 2015 at 7:39
  • 1
    @İsmailCeylan Check This Commented Jul 31, 2015 at 8:45

2 Answers 2

1

Simply use instead

'~\(.*?\)~'

and it'll be done

Regex101 Eval

Sign up to request clarification or add additional context in comments.

Comments

0

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.

'~\(.*?\)~'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.