PHP preg_match() doesn't match all subpatternsHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPRegular expression to match a line that doesn't contain a wordHow to replace all occurrences of a string?RegEx match open tags except XHTML self-contained tagsReference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Unexpected subpatterns captured by preg_matchReference - What does this error mean in PHP?Why shouldn't I use mysql_* functions in PHP?

How can I solve for the intersection points of two ellipses?

Three legged NOT gate? What is this symbol?

Acceptable to cut steak before searing?

how to differentiate when a child lwc component is called twice in parent component?

How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

Why does Intel's Haswell chip allow FP multiplication to be twice as fast as addition?

Ex-contractor published company source code and secrets online

Extremely casual way to make requests to very close friends

How does "Te vas a cansar" mean "You're going to get tired"?

Write an interpreter for *

What game uses dice with sides powers of 2?

Y2K... in 2019?

Can you castle with a "ghost" rook?

Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout

Does this Foo machine halt?

What are the conventions for transcribing Semitic languages into Greek?

How to create all combinations from a nested list while preserving the structure using R?

How are you supposed to know the strumming pattern for a song from the "chord sheet music"?

changing number of arguments to a function in secondary evaluation

Best gun to modify into a monsterhunter weapon?

Packages "LibertinusT1Math" raises error if compiled with XeLaTeX

Resonance and mesomeric effect

Are there any financial disadvantages to living significantly "below your means"?



PHP preg_match() doesn't match all subpatterns


How can I prevent SQL injection in PHP?Deleting an element from an array in PHPRegular expression to match a line that doesn't contain a wordHow to replace all occurrences of a string?RegEx match open tags except XHTML self-contained tagsReference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Unexpected subpatterns captured by preg_matchReference - What does this error mean in PHP?Why shouldn't I use mysql_* functions in PHP?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a preg_match() which matches the pattern but doesn't receive the expected matches (in third param).



My regex patterns have multiple subpatterns.



$pattern = "~^&multi&[^&]+(&(?:(p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&(?:(p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";

$string = "&multi&mickael&p-23&george&page-34";

preg_match($pattern, $string, $matches);


This is what $matches contains:



Array
(
[0] => &multi&mickael&p-23&george&page-34
[1] => &p-23
[2] => p-23
[sad] =>
[3] => 23
[4] =>
[5] => &page-34
[6] => page-34
[gogosi] => 34
[7] =>
[8] => 34
)


The problem is [sad] should have 23 value.

If I don't include in $string second page (page-34), 'cause is optional [...]



$string = "&multi&mickael&p-23&george";


[...] I have good $matches 'cause my [sad] got his value:



Array
(
[0] => &multi&mickael&p-23&george
[1] => &p-23
[2] => p-23
[sad] => 23
[3] => 23
)


But I want regex to return properly value even when I have both paginations in $string.



What to do such that all subpatterns will have their value ?



Note: Words as ('p', 'page') are only examples. Can be any words there.



Note: Above data is just an example. Don't give me workaround solutions, but something good for any input data.










share|improve this question
































    0















    I have a preg_match() which matches the pattern but doesn't receive the expected matches (in third param).



    My regex patterns have multiple subpatterns.



    $pattern = "~^&multi&[^&]+(&(?:(p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&(?:(p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";

    $string = "&multi&mickael&p-23&george&page-34";

    preg_match($pattern, $string, $matches);


    This is what $matches contains:



    Array
    (
    [0] => &multi&mickael&p-23&george&page-34
    [1] => &p-23
    [2] => p-23
    [sad] =>
    [3] => 23
    [4] =>
    [5] => &page-34
    [6] => page-34
    [gogosi] => 34
    [7] =>
    [8] => 34
    )


    The problem is [sad] should have 23 value.

    If I don't include in $string second page (page-34), 'cause is optional [...]



    $string = "&multi&mickael&p-23&george";


    [...] I have good $matches 'cause my [sad] got his value:



    Array
    (
    [0] => &multi&mickael&p-23&george
    [1] => &p-23
    [2] => p-23
    [sad] => 23
    [3] => 23
    )


    But I want regex to return properly value even when I have both paginations in $string.



    What to do such that all subpatterns will have their value ?



    Note: Words as ('p', 'page') are only examples. Can be any words there.



    Note: Above data is just an example. Don't give me workaround solutions, but something good for any input data.










    share|improve this question




























      0












      0








      0








      I have a preg_match() which matches the pattern but doesn't receive the expected matches (in third param).



      My regex patterns have multiple subpatterns.



      $pattern = "~^&multi&[^&]+(&(?:(p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&(?:(p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";

      $string = "&multi&mickael&p-23&george&page-34";

      preg_match($pattern, $string, $matches);


      This is what $matches contains:



      Array
      (
      [0] => &multi&mickael&p-23&george&page-34
      [1] => &p-23
      [2] => p-23
      [sad] =>
      [3] => 23
      [4] =>
      [5] => &page-34
      [6] => page-34
      [gogosi] => 34
      [7] =>
      [8] => 34
      )


      The problem is [sad] should have 23 value.

      If I don't include in $string second page (page-34), 'cause is optional [...]



      $string = "&multi&mickael&p-23&george";


      [...] I have good $matches 'cause my [sad] got his value:



      Array
      (
      [0] => &multi&mickael&p-23&george
      [1] => &p-23
      [2] => p-23
      [sad] => 23
      [3] => 23
      )


      But I want regex to return properly value even when I have both paginations in $string.



      What to do such that all subpatterns will have their value ?



      Note: Words as ('p', 'page') are only examples. Can be any words there.



      Note: Above data is just an example. Don't give me workaround solutions, but something good for any input data.










      share|improve this question
















      I have a preg_match() which matches the pattern but doesn't receive the expected matches (in third param).



      My regex patterns have multiple subpatterns.



      $pattern = "~^&multi&[^&]+(&(?:(p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&(?:(p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";

      $string = "&multi&mickael&p-23&george&page-34";

      preg_match($pattern, $string, $matches);


      This is what $matches contains:



      Array
      (
      [0] => &multi&mickael&p-23&george&page-34
      [1] => &p-23
      [2] => p-23
      [sad] =>
      [3] => 23
      [4] =>
      [5] => &page-34
      [6] => page-34
      [gogosi] => 34
      [7] =>
      [8] => 34
      )


      The problem is [sad] should have 23 value.

      If I don't include in $string second page (page-34), 'cause is optional [...]



      $string = "&multi&mickael&p-23&george";


      [...] I have good $matches 'cause my [sad] got his value:



      Array
      (
      [0] => &multi&mickael&p-23&george
      [1] => &p-23
      [2] => p-23
      [sad] => 23
      [3] => 23
      )


      But I want regex to return properly value even when I have both paginations in $string.



      What to do such that all subpatterns will have their value ?



      Note: Words as ('p', 'page') are only examples. Can be any words there.



      Note: Above data is just an example. Don't give me workaround solutions, but something good for any input data.







      php regex match preg-match






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 8:58







      Valentin Tanasescu

















      asked Mar 27 at 8:37









      Valentin TanasescuValentin Tanasescu

      809 bronze badges




      809 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          You may use a branch reset group, (?|...|...):



          '~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J'


          See the regex demo.



          See the PHP demo:



          $pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";
          $string = "&multi&mickael&p-23&george&page-34";
          if (preg_match($pattern, $string, $matches))
          print_r($matches);



          Output:



          Array
          (
          [0] => &multi&mickael&p-23&george&page-34
          [1] => &p-23
          [2] => p-23
          [sad] => 23
          [3] => 23
          [4] => &page-34
          [5] => page-34
          [gogosi] => 34
          [6] => 34
          )





          share|improve this answer



























          • Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

            – Valentin Tanasescu
            Mar 27 at 8:54












          • @ValentinTanasescu See another answer now.

            – Wiktor Stribiżew
            Mar 27 at 9:01











          • Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

            – Valentin Tanasescu
            Mar 27 at 22:27











          • Why are never included even if that's what documentation says ?

            – Valentin Tanasescu
            Mar 27 at 22:31











          • @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

            – Wiktor Stribiżew
            Mar 27 at 22:36










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372836%2fphp-preg-match-doesnt-match-all-subpatterns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You may use a branch reset group, (?|...|...):



          '~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J'


          See the regex demo.



          See the PHP demo:



          $pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";
          $string = "&multi&mickael&p-23&george&page-34";
          if (preg_match($pattern, $string, $matches))
          print_r($matches);



          Output:



          Array
          (
          [0] => &multi&mickael&p-23&george&page-34
          [1] => &p-23
          [2] => p-23
          [sad] => 23
          [3] => 23
          [4] => &page-34
          [5] => page-34
          [gogosi] => 34
          [6] => 34
          )





          share|improve this answer



























          • Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

            – Valentin Tanasescu
            Mar 27 at 8:54












          • @ValentinTanasescu See another answer now.

            – Wiktor Stribiżew
            Mar 27 at 9:01











          • Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

            – Valentin Tanasescu
            Mar 27 at 22:27











          • Why are never included even if that's what documentation says ?

            – Valentin Tanasescu
            Mar 27 at 22:31











          • @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

            – Wiktor Stribiżew
            Mar 27 at 22:36















          1














          You may use a branch reset group, (?|...|...):



          '~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J'


          See the regex demo.



          See the PHP demo:



          $pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";
          $string = "&multi&mickael&p-23&george&page-34";
          if (preg_match($pattern, $string, $matches))
          print_r($matches);



          Output:



          Array
          (
          [0] => &multi&mickael&p-23&george&page-34
          [1] => &p-23
          [2] => p-23
          [sad] => 23
          [3] => 23
          [4] => &page-34
          [5] => page-34
          [gogosi] => 34
          [6] => 34
          )





          share|improve this answer



























          • Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

            – Valentin Tanasescu
            Mar 27 at 8:54












          • @ValentinTanasescu See another answer now.

            – Wiktor Stribiżew
            Mar 27 at 9:01











          • Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

            – Valentin Tanasescu
            Mar 27 at 22:27











          • Why are never included even if that's what documentation says ?

            – Valentin Tanasescu
            Mar 27 at 22:31











          • @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

            – Wiktor Stribiżew
            Mar 27 at 22:36













          1












          1








          1







          You may use a branch reset group, (?|...|...):



          '~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J'


          See the regex demo.



          See the PHP demo:



          $pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";
          $string = "&multi&mickael&p-23&george&page-34";
          if (preg_match($pattern, $string, $matches))
          print_r($matches);



          Output:



          Array
          (
          [0] => &multi&mickael&p-23&george&page-34
          [1] => &p-23
          [2] => p-23
          [sad] => 23
          [3] => 23
          [4] => &page-34
          [5] => page-34
          [gogosi] => 34
          [6] => 34
          )





          share|improve this answer















          You may use a branch reset group, (?|...|...):



          '~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J'


          See the regex demo.



          See the PHP demo:



          $pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]d*)|page-(?<sad>[1-9]d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]d*)|page-(?<gogosi>[1-9]d*))))?&?$~J";
          $string = "&multi&mickael&p-23&george&page-34";
          if (preg_match($pattern, $string, $matches))
          print_r($matches);



          Output:



          Array
          (
          [0] => &multi&mickael&p-23&george&page-34
          [1] => &p-23
          [2] => p-23
          [sad] => 23
          [3] => 23
          [4] => &page-34
          [5] => page-34
          [gogosi] => 34
          [6] => 34
          )






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 9:00

























          answered Mar 27 at 8:48









          Wiktor StribiżewWiktor Stribiżew

          351k16 gold badges168 silver badges249 bronze badges




          351k16 gold badges168 silver badges249 bronze badges















          • Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

            – Valentin Tanasescu
            Mar 27 at 8:54












          • @ValentinTanasescu See another answer now.

            – Wiktor Stribiżew
            Mar 27 at 9:01











          • Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

            – Valentin Tanasescu
            Mar 27 at 22:27











          • Why are never included even if that's what documentation says ?

            – Valentin Tanasescu
            Mar 27 at 22:31











          • @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

            – Wiktor Stribiżew
            Mar 27 at 22:36

















          • Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

            – Valentin Tanasescu
            Mar 27 at 8:54












          • @ValentinTanasescu See another answer now.

            – Wiktor Stribiżew
            Mar 27 at 9:01











          • Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

            – Valentin Tanasescu
            Mar 27 at 22:27











          • Why are never included even if that's what documentation says ?

            – Valentin Tanasescu
            Mar 27 at 22:31











          • @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

            – Wiktor Stribiżew
            Mar 27 at 22:36
















          Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

          – Valentin Tanasescu
          Mar 27 at 8:54






          Thank you for your answer. But words as ('p', 'page') are only examples. Can be any words there. Would help me a solution for any subpattern names or input data. Thanks a lot.

          – Valentin Tanasescu
          Mar 27 at 8:54














          @ValentinTanasescu See another answer now.

          – Wiktor Stribiżew
          Mar 27 at 9:01





          @ValentinTanasescu See another answer now.

          – Wiktor Stribiżew
          Mar 27 at 9:01













          Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

          – Valentin Tanasescu
          Mar 27 at 22:27





          Thank you very much. Can I also ask you why the fourth parameter PREG_UNMATCHED_AS_NULL doesn't include in $matches unmatched subpatterns with NULL value as should do?

          – Valentin Tanasescu
          Mar 27 at 22:27













          Why are never included even if that's what documentation says ?

          – Valentin Tanasescu
          Mar 27 at 22:31





          Why are never included even if that's what documentation says ?

          – Valentin Tanasescu
          Mar 27 at 22:31













          @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

          – Wiktor Stribiżew
          Mar 27 at 22:36





          @ValentinTanasescu Sorry, I might have confused the exact behavior. Looks like it is a new constant and might be buggy.

          – Wiktor Stribiżew
          Mar 27 at 22:36








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372836%2fphp-preg-match-doesnt-match-all-subpatterns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript