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?
regex
add a comment |
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?
regex
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
add a comment |
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?
regex
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
regex
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
add a comment |
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
add a comment |
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".
add a comment |
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".
add a comment |
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".
add a comment |
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".
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".
answered Nov 10 at 16:04
Toto
63.8k175697
63.8k175697
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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