How to transform regexp to list











up vote
1
down vote

favorite
1












I've got a regexp string with only | and () like :



(Hello|Hi) my name is (Bob|Robert)



And I would like to have the complete list of string who match the regexp :



Hello my name is Bob
Hello my name is Robert
Hi my name is Bob
Hi my name is Robert



Is it a tool (librairy) who already do this ?



My first problem is to split the regexp string into a array of array like :



[['Hello','Hi'],'my name is' ,['Bob','Robert']]









share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    I've got a regexp string with only | and () like :



    (Hello|Hi) my name is (Bob|Robert)



    And I would like to have the complete list of string who match the regexp :



    Hello my name is Bob
    Hello my name is Robert
    Hi my name is Bob
    Hi my name is Robert



    Is it a tool (librairy) who already do this ?



    My first problem is to split the regexp string into a array of array like :



    [['Hello','Hi'],'my name is' ,['Bob','Robert']]









    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I've got a regexp string with only | and () like :



      (Hello|Hi) my name is (Bob|Robert)



      And I would like to have the complete list of string who match the regexp :



      Hello my name is Bob
      Hello my name is Robert
      Hi my name is Bob
      Hi my name is Robert



      Is it a tool (librairy) who already do this ?



      My first problem is to split the regexp string into a array of array like :



      [['Hello','Hi'],'my name is' ,['Bob','Robert']]









      share|improve this question













      I've got a regexp string with only | and () like :



      (Hello|Hi) my name is (Bob|Robert)



      And I would like to have the complete list of string who match the regexp :



      Hello my name is Bob
      Hello my name is Robert
      Hi my name is Bob
      Hi my name is Robert



      Is it a tool (librairy) who already do this ?



      My first problem is to split the regexp string into a array of array like :



      [['Hello','Hi'],'my name is' ,['Bob','Robert']]






      python string split






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 7:29









      axel584

      1646




      1646
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Try exrex, think that should work for you



          Simple script



          import exrex
          print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))


          Output



          → python new_test.py
          ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
          name is Robert']


          https://github.com/asciimoo/exrex






          share|improve this answer




























            up vote
            0
            down vote













            Do it with regex:-)



            re.split(r"((.+?|.+?))",s)
            Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
            # and for each string in the list:
            re.split(r"((.+?)|(.+?))",'(Hello|Hi)')
            Out: ['', 'Hello', 'Hi', '']





            share|improve this answer




























              up vote
              0
              down vote













              You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace



              input_string = "(Hello|Hi) my name is (Bob|Robert)"
              split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
              print ([i.strip().split("|") for i in split_string])

              #Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]




              I hope this helps!





              If you need the final solution to your query then use below code:



              from itertools import product
              input_string = "(Hello|Hi) my name is (Bob|Robert)"
              split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
              jj = [i.strip().split("|") for i in split_string]
              kk = list(product(*jj))
              print ([" ".join(i) for i in kk])
              #output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']


              The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"






              share|improve this answer























                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',
                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%2f53221452%2fhow-to-transform-regexp-to-list%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                Try exrex, think that should work for you



                Simple script



                import exrex
                print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))


                Output



                → python new_test.py
                ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
                name is Robert']


                https://github.com/asciimoo/exrex






                share|improve this answer

























                  up vote
                  2
                  down vote



                  accepted










                  Try exrex, think that should work for you



                  Simple script



                  import exrex
                  print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))


                  Output



                  → python new_test.py
                  ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
                  name is Robert']


                  https://github.com/asciimoo/exrex






                  share|improve this answer























                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    Try exrex, think that should work for you



                    Simple script



                    import exrex
                    print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))


                    Output



                    → python new_test.py
                    ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
                    name is Robert']


                    https://github.com/asciimoo/exrex






                    share|improve this answer












                    Try exrex, think that should work for you



                    Simple script



                    import exrex
                    print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))


                    Output



                    → python new_test.py
                    ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
                    name is Robert']


                    https://github.com/asciimoo/exrex







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 7:46









                    Richy

                    30318




                    30318
























                        up vote
                        0
                        down vote













                        Do it with regex:-)



                        re.split(r"((.+?|.+?))",s)
                        Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
                        # and for each string in the list:
                        re.split(r"((.+?)|(.+?))",'(Hello|Hi)')
                        Out: ['', 'Hello', 'Hi', '']





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Do it with regex:-)



                          re.split(r"((.+?|.+?))",s)
                          Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
                          # and for each string in the list:
                          re.split(r"((.+?)|(.+?))",'(Hello|Hi)')
                          Out: ['', 'Hello', 'Hi', '']





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Do it with regex:-)



                            re.split(r"((.+?|.+?))",s)
                            Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
                            # and for each string in the list:
                            re.split(r"((.+?)|(.+?))",'(Hello|Hi)')
                            Out: ['', 'Hello', 'Hi', '']





                            share|improve this answer












                            Do it with regex:-)



                            re.split(r"((.+?|.+?))",s)
                            Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
                            # and for each string in the list:
                            re.split(r"((.+?)|(.+?))",'(Hello|Hi)')
                            Out: ['', 'Hello', 'Hi', '']






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 7:46









                            kantal

                            60717




                            60717






















                                up vote
                                0
                                down vote













                                You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace



                                input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                print ([i.strip().split("|") for i in split_string])

                                #Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]




                                I hope this helps!





                                If you need the final solution to your query then use below code:



                                from itertools import product
                                input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                jj = [i.strip().split("|") for i in split_string]
                                kk = list(product(*jj))
                                print ([" ".join(i) for i in kk])
                                #output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']


                                The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"






                                share|improve this answer



























                                  up vote
                                  0
                                  down vote













                                  You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace



                                  input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                  split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                  print ([i.strip().split("|") for i in split_string])

                                  #Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]




                                  I hope this helps!





                                  If you need the final solution to your query then use below code:



                                  from itertools import product
                                  input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                  split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                  jj = [i.strip().split("|") for i in split_string]
                                  kk = list(product(*jj))
                                  print ([" ".join(i) for i in kk])
                                  #output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']


                                  The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace



                                    input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                    split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                    print ([i.strip().split("|") for i in split_string])

                                    #Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]




                                    I hope this helps!





                                    If you need the final solution to your query then use below code:



                                    from itertools import product
                                    input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                    split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                    jj = [i.strip().split("|") for i in split_string]
                                    kk = list(product(*jj))
                                    print ([" ".join(i) for i in kk])
                                    #output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']


                                    The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"






                                    share|improve this answer














                                    You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace



                                    input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                    split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                    print ([i.strip().split("|") for i in split_string])

                                    #Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]




                                    I hope this helps!





                                    If you need the final solution to your query then use below code:



                                    from itertools import product
                                    input_string = "(Hello|Hi) my name is (Bob|Robert)"
                                    split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
                                    jj = [i.strip().split("|") for i in split_string]
                                    kk = list(product(*jj))
                                    print ([" ".join(i) for i in kk])
                                    #output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']


                                    The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 9 at 11:46

























                                    answered Nov 9 at 9:35









                                    Akash Swain

                                    813




                                    813






























                                        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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2f53221452%2fhow-to-transform-regexp-to-list%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







                                        這個網誌中的熱門文章

                                        Academy of Television Arts & Sciences

                                        L'Équipe

                                        1995 France bombings