Regex to find match any combination of 3 terms











up vote
-1
down vote

favorite












I need to design a regex which will match any combination of n words, without duplicates.



E.g. the regex for the words "she" "is" "happy" would match "she is happy", "happy she is" but not "she is is happy" or "she is".



Can I do this with Regex for should I use a custom algorithm?










share|improve this question
























  • Does it have to be done with RegEx?
    – Wai Ha Lee
    Nov 10 at 15:07










  • I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
    – Infamous
    Nov 10 at 15:08












  • No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
    – Mark Micallef
    Nov 10 at 15:10















up vote
-1
down vote

favorite












I need to design a regex which will match any combination of n words, without duplicates.



E.g. the regex for the words "she" "is" "happy" would match "she is happy", "happy she is" but not "she is is happy" or "she is".



Can I do this with Regex for should I use a custom algorithm?










share|improve this question
























  • Does it have to be done with RegEx?
    – Wai Ha Lee
    Nov 10 at 15:07










  • I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
    – Infamous
    Nov 10 at 15:08












  • No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
    – Mark Micallef
    Nov 10 at 15:10













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I need to design a regex which will match any combination of n words, without duplicates.



E.g. the regex for the words "she" "is" "happy" would match "she is happy", "happy she is" but not "she is is happy" or "she is".



Can I do this with Regex for should I use a custom algorithm?










share|improve this question















I need to design a regex which will match any combination of n words, without duplicates.



E.g. the regex for the words "she" "is" "happy" would match "she is happy", "happy she is" but not "she is is happy" or "she is".



Can I do this with Regex for should I use a custom algorithm?







regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:06









Wai Ha Lee

5,630123660




5,630123660










asked Nov 10 at 15:02









Mark Micallef

5162721




5162721












  • Does it have to be done with RegEx?
    – Wai Ha Lee
    Nov 10 at 15:07










  • I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
    – Infamous
    Nov 10 at 15:08












  • No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
    – Mark Micallef
    Nov 10 at 15:10


















  • Does it have to be done with RegEx?
    – Wai Ha Lee
    Nov 10 at 15:07










  • I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
    – Infamous
    Nov 10 at 15:08












  • No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
    – Mark Micallef
    Nov 10 at 15:10
















Does it have to be done with RegEx?
– Wai Ha Lee
Nov 10 at 15:07




Does it have to be done with RegEx?
– Wai Ha Lee
Nov 10 at 15:07












I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
– Infamous
Nov 10 at 15:08






I too agree with @WaiHaLee, It is simply getting the list of words for the input text and checking if the list contains all required terms
– Infamous
Nov 10 at 15:08














No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
– Mark Micallef
Nov 10 at 15:10




No it doesn't. I just figured a regex engine is likely to be more efficient than whatever I write. My fallback is to do it myself.
– Mark Micallef
Nov 10 at 15:10












1 Answer
1






active

oldest

votes

















up vote
2
down vote













This match she is happy in any order but not duplicate word:



^(?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$)(?=(?:(?!bisb).)*bisb(?:(?!bisb).)*$)(?=(?:(?!bhappyb).)*bhappyb(?:(?!bhappyb).)*$).*$


DEMO



Let's explain the first part (i.e. (?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$))



This make sure we have one and only one "she" anywhere in the phrase.



(?=                     # start lookahead
(?: # non capture group
(?!bsheb) # negative lookahead, make sure we don't have "she"
. # any character
)* # end group, may appear 0 or more times
bsheb # literally "she" surounded by word boundaries
(?: # non capture group
(?!bsheb) # negative lookahead, make sure we don't have "she"
. # any character
)* # end group, may appear 0 or more times
$
)


Same explanation for the other words "is" and "happy".






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%2f53240208%2fregex-to-find-match-any-combination-of-3-terms%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    This match she is happy in any order but not duplicate word:



    ^(?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$)(?=(?:(?!bisb).)*bisb(?:(?!bisb).)*$)(?=(?:(?!bhappyb).)*bhappyb(?:(?!bhappyb).)*$).*$


    DEMO



    Let's explain the first part (i.e. (?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$))



    This make sure we have one and only one "she" anywhere in the phrase.



    (?=                     # start lookahead
    (?: # non capture group
    (?!bsheb) # negative lookahead, make sure we don't have "she"
    . # any character
    )* # end group, may appear 0 or more times
    bsheb # literally "she" surounded by word boundaries
    (?: # non capture group
    (?!bsheb) # negative lookahead, make sure we don't have "she"
    . # any character
    )* # end group, may appear 0 or more times
    $
    )


    Same explanation for the other words "is" and "happy".






    share|improve this answer

























      up vote
      2
      down vote













      This match she is happy in any order but not duplicate word:



      ^(?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$)(?=(?:(?!bisb).)*bisb(?:(?!bisb).)*$)(?=(?:(?!bhappyb).)*bhappyb(?:(?!bhappyb).)*$).*$


      DEMO



      Let's explain the first part (i.e. (?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$))



      This make sure we have one and only one "she" anywhere in the phrase.



      (?=                     # start lookahead
      (?: # non capture group
      (?!bsheb) # negative lookahead, make sure we don't have "she"
      . # any character
      )* # end group, may appear 0 or more times
      bsheb # literally "she" surounded by word boundaries
      (?: # non capture group
      (?!bsheb) # negative lookahead, make sure we don't have "she"
      . # any character
      )* # end group, may appear 0 or more times
      $
      )


      Same explanation for the other words "is" and "happy".






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        This match she is happy in any order but not duplicate word:



        ^(?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$)(?=(?:(?!bisb).)*bisb(?:(?!bisb).)*$)(?=(?:(?!bhappyb).)*bhappyb(?:(?!bhappyb).)*$).*$


        DEMO



        Let's explain the first part (i.e. (?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$))



        This make sure we have one and only one "she" anywhere in the phrase.



        (?=                     # start lookahead
        (?: # non capture group
        (?!bsheb) # negative lookahead, make sure we don't have "she"
        . # any character
        )* # end group, may appear 0 or more times
        bsheb # literally "she" surounded by word boundaries
        (?: # non capture group
        (?!bsheb) # negative lookahead, make sure we don't have "she"
        . # any character
        )* # end group, may appear 0 or more times
        $
        )


        Same explanation for the other words "is" and "happy".






        share|improve this answer












        This match she is happy in any order but not duplicate word:



        ^(?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$)(?=(?:(?!bisb).)*bisb(?:(?!bisb).)*$)(?=(?:(?!bhappyb).)*bhappyb(?:(?!bhappyb).)*$).*$


        DEMO



        Let's explain the first part (i.e. (?=(?:(?!bsheb).)*bsheb(?:(?!bsheb).)*$))



        This make sure we have one and only one "she" anywhere in the phrase.



        (?=                     # start lookahead
        (?: # non capture group
        (?!bsheb) # negative lookahead, make sure we don't have "she"
        . # any character
        )* # end group, may appear 0 or more times
        bsheb # literally "she" surounded by word boundaries
        (?: # non capture group
        (?!bsheb) # negative lookahead, make sure we don't have "she"
        . # any character
        )* # end group, may appear 0 or more times
        $
        )


        Same explanation for the other words "is" and "happy".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 16:04









        Toto

        63.8k175697




        63.8k175697






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240208%2fregex-to-find-match-any-combination-of-3-terms%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python