Regex: Specified words in any order












10















I'm not good at regex, trying to make 2 regex.



Regex1:




All specified words in any order but nothing else. (repetition
allowed).




Regex2:




All specified words in any order but nothing else.
(repetition not allowed).




Words:



aaa, bbb, ccc


Strings:



aaa ccc bbb
aaa ccc
aaa bbb ddd ccc
bbb aaa bbb ccc


Regex1 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
false -> repetition not allowed


Regex2 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
true -> all word present in any order and repetition is allowed


My Attempt



/^(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).*$/


Asking for learning purpose so please elaborate it.










share|improve this question

























  • So some chars like spaces are allowed to exist between words? What else could be there?

    – revo
    2 days ago











  • only spaces, newline, tabs are allowed.

    – shajji
    2 days ago








  • 1





    Are you sure about newlines to exist between words?

    – revo
    2 days ago








  • 1





    Please check this regex101.com/r/Olu2kI/1

    – revo
    2 days ago






  • 1





    Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

    – Eric Duminil
    2 days ago
















10















I'm not good at regex, trying to make 2 regex.



Regex1:




All specified words in any order but nothing else. (repetition
allowed).




Regex2:




All specified words in any order but nothing else.
(repetition not allowed).




Words:



aaa, bbb, ccc


Strings:



aaa ccc bbb
aaa ccc
aaa bbb ddd ccc
bbb aaa bbb ccc


Regex1 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
false -> repetition not allowed


Regex2 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
true -> all word present in any order and repetition is allowed


My Attempt



/^(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).*$/


Asking for learning purpose so please elaborate it.










share|improve this question

























  • So some chars like spaces are allowed to exist between words? What else could be there?

    – revo
    2 days ago











  • only spaces, newline, tabs are allowed.

    – shajji
    2 days ago








  • 1





    Are you sure about newlines to exist between words?

    – revo
    2 days ago








  • 1





    Please check this regex101.com/r/Olu2kI/1

    – revo
    2 days ago






  • 1





    Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

    – Eric Duminil
    2 days ago














10












10








10


0






I'm not good at regex, trying to make 2 regex.



Regex1:




All specified words in any order but nothing else. (repetition
allowed).




Regex2:




All specified words in any order but nothing else.
(repetition not allowed).




Words:



aaa, bbb, ccc


Strings:



aaa ccc bbb
aaa ccc
aaa bbb ddd ccc
bbb aaa bbb ccc


Regex1 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
false -> repetition not allowed


Regex2 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
true -> all word present in any order and repetition is allowed


My Attempt



/^(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).*$/


Asking for learning purpose so please elaborate it.










share|improve this question
















I'm not good at regex, trying to make 2 regex.



Regex1:




All specified words in any order but nothing else. (repetition
allowed).




Regex2:




All specified words in any order but nothing else.
(repetition not allowed).




Words:



aaa, bbb, ccc


Strings:



aaa ccc bbb
aaa ccc
aaa bbb ddd ccc
bbb aaa bbb ccc


Regex1 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
false -> repetition not allowed


Regex2 evaluate above strings as:



true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
true -> all word present in any order and repetition is allowed


My Attempt



/^(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).*$/


Asking for learning purpose so please elaborate it.







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







shajji

















asked 2 days ago









shajjishajji

494212




494212













  • So some chars like spaces are allowed to exist between words? What else could be there?

    – revo
    2 days ago











  • only spaces, newline, tabs are allowed.

    – shajji
    2 days ago








  • 1





    Are you sure about newlines to exist between words?

    – revo
    2 days ago








  • 1





    Please check this regex101.com/r/Olu2kI/1

    – revo
    2 days ago






  • 1





    Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

    – Eric Duminil
    2 days ago



















  • So some chars like spaces are allowed to exist between words? What else could be there?

    – revo
    2 days ago











  • only spaces, newline, tabs are allowed.

    – shajji
    2 days ago








  • 1





    Are you sure about newlines to exist between words?

    – revo
    2 days ago








  • 1





    Please check this regex101.com/r/Olu2kI/1

    – revo
    2 days ago






  • 1





    Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

    – Eric Duminil
    2 days ago

















So some chars like spaces are allowed to exist between words? What else could be there?

– revo
2 days ago





So some chars like spaces are allowed to exist between words? What else could be there?

– revo
2 days ago













only spaces, newline, tabs are allowed.

– shajji
2 days ago







only spaces, newline, tabs are allowed.

– shajji
2 days ago






1




1





Are you sure about newlines to exist between words?

– revo
2 days ago







Are you sure about newlines to exist between words?

– revo
2 days ago






1




1





Please check this regex101.com/r/Olu2kI/1

– revo
2 days ago





Please check this regex101.com/r/Olu2kI/1

– revo
2 days ago




1




1





Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

– Eric Duminil
2 days ago





Just because you can use a regex doesn't mean you should. var input = "ccc aaa ccc bbb"; var words = input.split(" "); var uniqueWords = Array.from(new Set(words)); console.log(uniqueWords.sort().join(" ") === "aaa bbb ccc");

– Eric Duminil
2 days ago












4 Answers
4






active

oldest

votes


















2














For Regex 1:






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





Your code already does part of the trick. Your positive lookaheads check that all words appear somewhere, however not, that they are the only words present. To achieve this, I added the circumflex (^) at the beginning to detect the start of the string. Then, the non capturing group of b(?:aaa|bbb|ccc)b, to detect the first instance of any word.
This is then followed by any number of words, preceded by at least one space (?:s+b(?:aaa|bbb|ccc)b)*, basically the same pattern, but with the s+ in front, and wrapped in a *. And then we need the string to end somewhere. This is done with the dollar sign $.



For Regex 2:



The basic strategy is the same. You would just check with a negative lookahead, that the matched string does not exist again:






//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





First, we have the positive lookahead (?=.*?bwordb) to see that word exists. We follow that by the negative lookahead (?!.*?baaab.*?baaab) to see, the word does not exist multiple times. Repeat for all words. Presto!



Update: Instead of checking the specific words aren't repeated, we can also check that NO word is repeated by using the (?!.*?b(w+)b.*?b1b) construct. This makes the regex more concise. Thanks to @revo for pointing it out.






share|improve this answer





















  • 1





    You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

    – revo
    2 days ago








  • 1





    and you shouldn't use s otherwise it will mess up with multiline input strings.

    – revo
    2 days ago






  • 1





    Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

    – Christoph Herold
    2 days ago








  • 1





    See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

    – revo
    2 days ago






  • 1





    Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

    – revo
    2 days ago



















3














Without repitition regex101



^(?:(aaa|bbb|ccc)(?!.*?b1b) ?b){3}$


And with repitition regex101



^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?:(aaa|bbb|ccc) ?b)+$


Two more ideas. Regex explanation at regex101 on the right side.






share|improve this answer
























  • wow great :) your both regex are to concise

    – shajji
    yesterday





















2














why do you need regex to perform this function though? you could achieve what you want easily by first splitting the strings with delimiter ",".
You can then create a dictionary object with the words that you are seeking as the keys and values defaulted to -1



Regex 2 can be achieved by looping through the input words and check if they exists as keys in the dictionary object.
Regex 1 can be achieved similarly, just that when a key is matched to the input word, its value would then be changed to 1 and when it is next visited, a false match can be returned.






share|improve this answer








New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

    – shajji
    2 days ago



















1














Do not use regex for uniqueness.



But for separate words in regex, you can use b



Example: /b(word1|word2|word3)b/






share|improve this answer
























  • what about order ?

    – shajji
    2 days ago






  • 1





    @shajji it will work regardless of order. | ( alternation ) is same as Logical OR

    – Code Maniac
    2 days ago











  • u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

    – shajji
    2 days ago













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%2f55116510%2fregex-specified-words-in-any-order%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














For Regex 1:






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





Your code already does part of the trick. Your positive lookaheads check that all words appear somewhere, however not, that they are the only words present. To achieve this, I added the circumflex (^) at the beginning to detect the start of the string. Then, the non capturing group of b(?:aaa|bbb|ccc)b, to detect the first instance of any word.
This is then followed by any number of words, preceded by at least one space (?:s+b(?:aaa|bbb|ccc)b)*, basically the same pattern, but with the s+ in front, and wrapped in a *. And then we need the string to end somewhere. This is done with the dollar sign $.



For Regex 2:



The basic strategy is the same. You would just check with a negative lookahead, that the matched string does not exist again:






//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





First, we have the positive lookahead (?=.*?bwordb) to see that word exists. We follow that by the negative lookahead (?!.*?baaab.*?baaab) to see, the word does not exist multiple times. Repeat for all words. Presto!



Update: Instead of checking the specific words aren't repeated, we can also check that NO word is repeated by using the (?!.*?b(w+)b.*?b1b) construct. This makes the regex more concise. Thanks to @revo for pointing it out.






share|improve this answer





















  • 1





    You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

    – revo
    2 days ago








  • 1





    and you shouldn't use s otherwise it will mess up with multiline input strings.

    – revo
    2 days ago






  • 1





    Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

    – Christoph Herold
    2 days ago








  • 1





    See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

    – revo
    2 days ago






  • 1





    Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

    – revo
    2 days ago
















2














For Regex 1:






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





Your code already does part of the trick. Your positive lookaheads check that all words appear somewhere, however not, that they are the only words present. To achieve this, I added the circumflex (^) at the beginning to detect the start of the string. Then, the non capturing group of b(?:aaa|bbb|ccc)b, to detect the first instance of any word.
This is then followed by any number of words, preceded by at least one space (?:s+b(?:aaa|bbb|ccc)b)*, basically the same pattern, but with the s+ in front, and wrapped in a *. And then we need the string to end somewhere. This is done with the dollar sign $.



For Regex 2:



The basic strategy is the same. You would just check with a negative lookahead, that the matched string does not exist again:






//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





First, we have the positive lookahead (?=.*?bwordb) to see that word exists. We follow that by the negative lookahead (?!.*?baaab.*?baaab) to see, the word does not exist multiple times. Repeat for all words. Presto!



Update: Instead of checking the specific words aren't repeated, we can also check that NO word is repeated by using the (?!.*?b(w+)b.*?b1b) construct. This makes the regex more concise. Thanks to @revo for pointing it out.






share|improve this answer





















  • 1





    You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

    – revo
    2 days ago








  • 1





    and you shouldn't use s otherwise it will mess up with multiline input strings.

    – revo
    2 days ago






  • 1





    Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

    – Christoph Herold
    2 days ago








  • 1





    See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

    – revo
    2 days ago






  • 1





    Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

    – revo
    2 days ago














2












2








2







For Regex 1:






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





Your code already does part of the trick. Your positive lookaheads check that all words appear somewhere, however not, that they are the only words present. To achieve this, I added the circumflex (^) at the beginning to detect the start of the string. Then, the non capturing group of b(?:aaa|bbb|ccc)b, to detect the first instance of any word.
This is then followed by any number of words, preceded by at least one space (?:s+b(?:aaa|bbb|ccc)b)*, basically the same pattern, but with the s+ in front, and wrapped in a *. And then we need the string to end somewhere. This is done with the dollar sign $.



For Regex 2:



The basic strategy is the same. You would just check with a negative lookahead, that the matched string does not exist again:






//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





First, we have the positive lookahead (?=.*?bwordb) to see that word exists. We follow that by the negative lookahead (?!.*?baaab.*?baaab) to see, the word does not exist multiple times. Repeat for all words. Presto!



Update: Instead of checking the specific words aren't repeated, we can also check that NO word is repeated by using the (?!.*?b(w+)b.*?b1b) construct. This makes the regex more concise. Thanks to @revo for pointing it out.






share|improve this answer















For Regex 1:






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





Your code already does part of the trick. Your positive lookaheads check that all words appear somewhere, however not, that they are the only words present. To achieve this, I added the circumflex (^) at the beginning to detect the start of the string. Then, the non capturing group of b(?:aaa|bbb|ccc)b, to detect the first instance of any word.
This is then followed by any number of words, preceded by at least one space (?:s+b(?:aaa|bbb|ccc)b)*, basically the same pattern, but with the s+ in front, and wrapped in a *. And then we need the string to end somewhere. This is done with the dollar sign $.



For Regex 2:



The basic strategy is the same. You would just check with a negative lookahead, that the matched string does not exist again:






//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





First, we have the positive lookahead (?=.*?bwordb) to see that word exists. We follow that by the negative lookahead (?!.*?baaab.*?baaab) to see, the word does not exist multiple times. Repeat for all words. Presto!



Update: Instead of checking the specific words aren't repeated, we can also check that NO word is repeated by using the (?!.*?b(w+)b.*?b1b) construct. This makes the regex more concise. Thanks to @revo for pointing it out.






var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa ddd bbb');
res.innerText += ', ' + re.test('ccc bbb ccc');

<div id="result"></div>





//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>





//var re = /^(?=.*?baaab)(?!.*?baaab.*?baaab)(?=.*?bbbbb)(?!.*?bbbbb.*?bbbbb)(?=.*?bcccb)(?!.*?bcccb.*?bcccb)b(?:aaa|bbb|ccc)b(?:s+b(?:aaa|bbb|ccc)b)*$/;
// optimized version, see comments
var re = /^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?!.*?b(w+)b.*?b1b)b(?:aaa|bbb|ccc)b(?: +b(?:aaa|bbb|ccc)b)*$/;
var res = document.getElementById('result');
res.innerText += re.test('aaa ccc bbb');
res.innerText += ', ' + re.test('aaa ccc ddd');
res.innerText += ', ' + re.test('aaa bbb aaa');
res.innerText += ', ' + re.test('aaa ccc bbb ccc');

<div id="result"></div>






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Christoph HeroldChristoph Herold

957516




957516








  • 1





    You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

    – revo
    2 days ago








  • 1





    and you shouldn't use s otherwise it will mess up with multiline input strings.

    – revo
    2 days ago






  • 1





    Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

    – Christoph Herold
    2 days ago








  • 1





    See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

    – revo
    2 days ago






  • 1





    Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

    – revo
    2 days ago














  • 1





    You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

    – revo
    2 days ago








  • 1





    and you shouldn't use s otherwise it will mess up with multiline input strings.

    – revo
    2 days ago






  • 1





    Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

    – Christoph Herold
    2 days ago








  • 1





    See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

    – revo
    2 days ago






  • 1





    Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

    – revo
    2 days ago








1




1





You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

– revo
2 days ago







You could remove all those negative lookaheads in favor of (?!.*?b(w+)b.*?b1b)

– revo
2 days ago






1




1





and you shouldn't use s otherwise it will mess up with multiline input strings.

– revo
2 days ago





and you shouldn't use s otherwise it will mess up with multiline input strings.

– revo
2 days ago




1




1





Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

– Christoph Herold
2 days ago







Ah, ok, I get it now. First look for all specific words, then check that NO word exists twice. Yes, nice optimization.

– Christoph Herold
2 days ago






1




1





See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

– revo
2 days ago





See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1

– revo
2 days ago




1




1





Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

– revo
2 days ago





Lookarounds orders isn't important at all. It is (?=.*baaab)(?=.*bbbbb)(?=.*bcccb)(?!.*?b(w+)b.*?1) but it could be (?!.*?b(w+)b.*?1)(?=.*baaab)(?=.*bbbbb)(?=.*bcccb).

– revo
2 days ago













3














Without repitition regex101



^(?:(aaa|bbb|ccc)(?!.*?b1b) ?b){3}$


And with repitition regex101



^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?:(aaa|bbb|ccc) ?b)+$


Two more ideas. Regex explanation at regex101 on the right side.






share|improve this answer
























  • wow great :) your both regex are to concise

    – shajji
    yesterday


















3














Without repitition regex101



^(?:(aaa|bbb|ccc)(?!.*?b1b) ?b){3}$


And with repitition regex101



^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?:(aaa|bbb|ccc) ?b)+$


Two more ideas. Regex explanation at regex101 on the right side.






share|improve this answer
























  • wow great :) your both regex are to concise

    – shajji
    yesterday
















3












3








3







Without repitition regex101



^(?:(aaa|bbb|ccc)(?!.*?b1b) ?b){3}$


And with repitition regex101



^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?:(aaa|bbb|ccc) ?b)+$


Two more ideas. Regex explanation at regex101 on the right side.






share|improve this answer













Without repitition regex101



^(?:(aaa|bbb|ccc)(?!.*?b1b) ?b){3}$


And with repitition regex101



^(?=.*?baaab)(?=.*?bbbbb)(?=.*?bcccb)(?:(aaa|bbb|ccc) ?b)+$


Two more ideas. Regex explanation at regex101 on the right side.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









bobble bubblebobble bubble

6,40611429




6,40611429













  • wow great :) your both regex are to concise

    – shajji
    yesterday





















  • wow great :) your both regex are to concise

    – shajji
    yesterday



















wow great :) your both regex are to concise

– shajji
yesterday







wow great :) your both regex are to concise

– shajji
yesterday













2














why do you need regex to perform this function though? you could achieve what you want easily by first splitting the strings with delimiter ",".
You can then create a dictionary object with the words that you are seeking as the keys and values defaulted to -1



Regex 2 can be achieved by looping through the input words and check if they exists as keys in the dictionary object.
Regex 1 can be achieved similarly, just that when a key is matched to the input word, its value would then be changed to 1 and when it is next visited, a false match can be returned.






share|improve this answer








New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

    – shajji
    2 days ago
















2














why do you need regex to perform this function though? you could achieve what you want easily by first splitting the strings with delimiter ",".
You can then create a dictionary object with the words that you are seeking as the keys and values defaulted to -1



Regex 2 can be achieved by looping through the input words and check if they exists as keys in the dictionary object.
Regex 1 can be achieved similarly, just that when a key is matched to the input word, its value would then be changed to 1 and when it is next visited, a false match can be returned.






share|improve this answer








New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

    – shajji
    2 days ago














2












2








2







why do you need regex to perform this function though? you could achieve what you want easily by first splitting the strings with delimiter ",".
You can then create a dictionary object with the words that you are seeking as the keys and values defaulted to -1



Regex 2 can be achieved by looping through the input words and check if they exists as keys in the dictionary object.
Regex 1 can be achieved similarly, just that when a key is matched to the input word, its value would then be changed to 1 and when it is next visited, a false match can be returned.






share|improve this answer








New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










why do you need regex to perform this function though? you could achieve what you want easily by first splitting the strings with delimiter ",".
You can then create a dictionary object with the words that you are seeking as the keys and values defaulted to -1



Regex 2 can be achieved by looping through the input words and check if they exists as keys in the dictionary object.
Regex 1 can be achieved similarly, just that when a key is matched to the input word, its value would then be changed to 1 and when it is next visited, a false match can be returned.







share|improve this answer








New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 2 days ago









shikai ngshikai ng

492




492




New contributor




shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






shikai ng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

    – shajji
    2 days ago



















  • u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

    – shajji
    2 days ago

















u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

– shajji
2 days ago





u r right, every validation can solve without regex but i'm intend to solve this with regex if possible. trying to learn advance regex

– shajji
2 days ago











1














Do not use regex for uniqueness.



But for separate words in regex, you can use b



Example: /b(word1|word2|word3)b/






share|improve this answer
























  • what about order ?

    – shajji
    2 days ago






  • 1





    @shajji it will work regardless of order. | ( alternation ) is same as Logical OR

    – Code Maniac
    2 days ago











  • u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

    – shajji
    2 days ago


















1














Do not use regex for uniqueness.



But for separate words in regex, you can use b



Example: /b(word1|word2|word3)b/






share|improve this answer
























  • what about order ?

    – shajji
    2 days ago






  • 1





    @shajji it will work regardless of order. | ( alternation ) is same as Logical OR

    – Code Maniac
    2 days ago











  • u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

    – shajji
    2 days ago
















1












1








1







Do not use regex for uniqueness.



But for separate words in regex, you can use b



Example: /b(word1|word2|word3)b/






share|improve this answer













Do not use regex for uniqueness.



But for separate words in regex, you can use b



Example: /b(word1|word2|word3)b/







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









SergejSergej

890413




890413













  • what about order ?

    – shajji
    2 days ago






  • 1





    @shajji it will work regardless of order. | ( alternation ) is same as Logical OR

    – Code Maniac
    2 days ago











  • u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

    – shajji
    2 days ago





















  • what about order ?

    – shajji
    2 days ago






  • 1





    @shajji it will work regardless of order. | ( alternation ) is same as Logical OR

    – Code Maniac
    2 days ago











  • u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

    – shajji
    2 days ago



















what about order ?

– shajji
2 days ago





what about order ?

– shajji
2 days ago




1




1





@shajji it will work regardless of order. | ( alternation ) is same as Logical OR

– Code Maniac
2 days ago





@shajji it will work regardless of order. | ( alternation ) is same as Logical OR

– Code Maniac
2 days ago













u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

– shajji
2 days ago







u r right, but i'm intend to do some other thing described in question. well thnx for your help :).

– shajji
2 days ago




















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%2f55116510%2fregex-specified-words-in-any-order%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

Callistus I

Tabula Rosettana

How to label and detect the document text images