Regex: Specified words in any order [on hold]
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 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
put on hold as too broad by CertainPerformance, il_raffa, greg-449, Oussema Aroua, GOTO 0 8 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 4 more comments
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 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
put on hold as too broad by CertainPerformance, il_raffa, greg-449, Oussema Aroua, GOTO 0 8 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
1
Are you sure about newlines to exist between words?
– revo
11 hours ago
1
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours 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
8 hours ago
|
show 4 more comments
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 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
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 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
javascript regex
edited 11 hours ago
shajji
asked 11 hours ago
shajjishajji
484211
484211
put on hold as too broad by CertainPerformance, il_raffa, greg-449, Oussema Aroua, GOTO 0 8 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as too broad by CertainPerformance, il_raffa, greg-449, Oussema Aroua, GOTO 0 8 hours ago
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
1
Are you sure about newlines to exist between words?
– revo
11 hours ago
1
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours 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
8 hours ago
|
show 4 more comments
2
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
1
Are you sure about newlines to exist between words?
– revo
11 hours ago
1
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours 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
8 hours ago
2
2
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
1
1
Are you sure about newlines to exist between words?
– revo
11 hours ago
Are you sure about newlines to exist between words?
– revo
11 hours ago
1
1
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours ago
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours 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
8 hours 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
8 hours ago
|
show 4 more comments
4 Answers
4
active
oldest
votes
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.
1
You could remove all those negative lookaheads in favor of(?!.*?b(w+)b.*?b1b)
– revo
10 hours ago
1
and you shouldn't usesotherwise it will mess up with multiline input strings.
– revo
10 hours 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
10 hours ago
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours ago
|
show 16 more comments
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.
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
11 hours ago
add a comment |
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.
add a comment |
Do not use regex for uniqueness.
But for separate words in regex, you can use b
Example: /b(word1|word2|word3)b/
what about order ?
– shajji
11 hours ago
1
@shajji it will work regardless of order.|( alternation ) is same as Logical OR
– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
You could remove all those negative lookaheads in favor of(?!.*?b(w+)b.*?b1b)
– revo
10 hours ago
1
and you shouldn't usesotherwise it will mess up with multiline input strings.
– revo
10 hours 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
10 hours ago
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours ago
|
show 16 more comments
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.
1
You could remove all those negative lookaheads in favor of(?!.*?b(w+)b.*?b1b)
– revo
10 hours ago
1
and you shouldn't usesotherwise it will mess up with multiline input strings.
– revo
10 hours 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
10 hours ago
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours ago
|
show 16 more comments
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.
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>edited 10 hours ago
answered 11 hours ago
Christoph HeroldChristoph Herold
902513
902513
1
You could remove all those negative lookaheads in favor of(?!.*?b(w+)b.*?b1b)
– revo
10 hours ago
1
and you shouldn't usesotherwise it will mess up with multiline input strings.
– revo
10 hours 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
10 hours ago
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours ago
|
show 16 more comments
1
You could remove all those negative lookaheads in favor of(?!.*?b(w+)b.*?b1b)
– revo
10 hours ago
1
and you shouldn't usesotherwise it will mess up with multiline input strings.
– revo
10 hours 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
10 hours ago
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours ago
1
1
You could remove all those negative lookaheads in favor of
(?!.*?b(w+)b.*?b1b)– revo
10 hours ago
You could remove all those negative lookaheads in favor of
(?!.*?b(w+)b.*?b1b)– revo
10 hours ago
1
1
and you shouldn't use
s otherwise it will mess up with multiline input strings.– revo
10 hours ago
and you shouldn't use
s otherwise it will mess up with multiline input strings.– revo
10 hours 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
10 hours 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
10 hours ago
1
1
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours ago
See it here to understand how your regex behaves on multiline input strings regex101.com/r/5EMjEN/1
– revo
10 hours 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
10 hours 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
10 hours ago
|
show 16 more comments
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.
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
11 hours ago
add a comment |
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.
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
11 hours ago
add a comment |
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.
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.
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.
answered 11 hours 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
11 hours ago
add a comment |
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
11 hours 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
11 hours 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
11 hours ago
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 9 hours ago
bobble bubblebobble bubble
6,39611429
6,39611429
add a comment |
add a comment |
Do not use regex for uniqueness.
But for separate words in regex, you can use b
Example: /b(word1|word2|word3)b/
what about order ?
– shajji
11 hours ago
1
@shajji it will work regardless of order.|( alternation ) is same as Logical OR
– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
add a comment |
Do not use regex for uniqueness.
But for separate words in regex, you can use b
Example: /b(word1|word2|word3)b/
what about order ?
– shajji
11 hours ago
1
@shajji it will work regardless of order.|( alternation ) is same as Logical OR
– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
add a comment |
Do not use regex for uniqueness.
But for separate words in regex, you can use b
Example: /b(word1|word2|word3)b/
Do not use regex for uniqueness.
But for separate words in regex, you can use b
Example: /b(word1|word2|word3)b/
answered 11 hours ago
SergejSergej
835413
835413
what about order ?
– shajji
11 hours ago
1
@shajji it will work regardless of order.|( alternation ) is same as Logical OR
– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
add a comment |
what about order ?
– shajji
11 hours ago
1
@shajji it will work regardless of order.|( alternation ) is same as Logical OR
– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
what about order ?
– shajji
11 hours ago
what about order ?
– shajji
11 hours ago
1
1
@shajji it will work regardless of order.
| ( alternation ) is same as Logical OR– Code Maniac
10 hours ago
@shajji it will work regardless of order.
| ( alternation ) is same as Logical OR– Code Maniac
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
u r right, but i'm intend to do some other thing described in question. well thnx for your help :).
– shajji
10 hours ago
add a comment |
2
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
11 hours ago
So some chars like spaces are allowed to exist between words? What else could be there?
– revo
11 hours ago
1
Are you sure about newlines to exist between words?
– revo
11 hours ago
1
Please check this regex101.com/r/Olu2kI/1
– revo
10 hours 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
8 hours ago