Regex to match string containing two names in any order
I need logical AND in regex.
something like
jack AND james
agree with following strings
'hi jack here is james'
'hi james here is jack'
regex string
add a comment |
I need logical AND in regex.
something like
jack AND james
agree with following strings
'hi jack here is james'
'hi james here is jack'
regex string
1
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44
add a comment |
I need logical AND in regex.
something like
jack AND james
agree with following strings
'hi jack here is james'
'hi james here is jack'
regex string
I need logical AND in regex.
something like
jack AND james
agree with following strings
'hi jack here is james'
'hi james here is jack'
regex string
regex string
edited Dec 9 '10 at 2:48
codaddict
347k64438490
347k64438490
asked Dec 8 '10 at 16:13
MelounMeloun
3,439114079
3,439114079
1
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44
add a comment |
1
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44
1
1
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44
add a comment |
4 Answers
4
active
oldest
votes
You can do checks using lookarounds:
^(?=.*bjackb)(?=.*bjamesb).*$
Test it.
This approach has the advantage that you can easily specify multiple conditions.
^(?=.*bjackb)(?=.*bjamesb)(?=.*bjasonb)(?=.*bjulesb).*$
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
vim
syntax:^(.*<jack>)@=(.*<james>@=).*$
orv^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to matchblah #tgif blah #friday blah
but^(?=.*bfridayb)(?=.*btgifb).*$
works fine.
– btleffler
Aug 24 '15 at 18:27
1
What doesb
means here?
– VarunAgw
Apr 26 '16 at 12:14
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
|
show 5 more comments
Try:
james.*jack
If you want both at the same time, then or
them:
james.*jack|jack.*james
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
add a comment |
Explanation of command that i am going to write:-
.
means any character, digit can come in place of .
*
means zero or more occurrences of thing written just previous to it.
|
means 'or'.
So,
james.*jack
would search james
, then any number of character until jack
comes.
Since you want either jack.*james
or james.*jack
Hence Command:
jack.*james|james.*jack
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
add a comment |
You can do:
bjackb.*bjamesb|bjamesb.*bjackb
add a comment |
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
);
);
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%2f4389644%2fregex-to-match-string-containing-two-names-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
You can do checks using lookarounds:
^(?=.*bjackb)(?=.*bjamesb).*$
Test it.
This approach has the advantage that you can easily specify multiple conditions.
^(?=.*bjackb)(?=.*bjamesb)(?=.*bjasonb)(?=.*bjulesb).*$
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
vim
syntax:^(.*<jack>)@=(.*<james>@=).*$
orv^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to matchblah #tgif blah #friday blah
but^(?=.*bfridayb)(?=.*btgifb).*$
works fine.
– btleffler
Aug 24 '15 at 18:27
1
What doesb
means here?
– VarunAgw
Apr 26 '16 at 12:14
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
|
show 5 more comments
You can do checks using lookarounds:
^(?=.*bjackb)(?=.*bjamesb).*$
Test it.
This approach has the advantage that you can easily specify multiple conditions.
^(?=.*bjackb)(?=.*bjamesb)(?=.*bjasonb)(?=.*bjulesb).*$
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
vim
syntax:^(.*<jack>)@=(.*<james>@=).*$
orv^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to matchblah #tgif blah #friday blah
but^(?=.*bfridayb)(?=.*btgifb).*$
works fine.
– btleffler
Aug 24 '15 at 18:27
1
What doesb
means here?
– VarunAgw
Apr 26 '16 at 12:14
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
|
show 5 more comments
You can do checks using lookarounds:
^(?=.*bjackb)(?=.*bjamesb).*$
Test it.
This approach has the advantage that you can easily specify multiple conditions.
^(?=.*bjackb)(?=.*bjamesb)(?=.*bjasonb)(?=.*bjulesb).*$
You can do checks using lookarounds:
^(?=.*bjackb)(?=.*bjamesb).*$
Test it.
This approach has the advantage that you can easily specify multiple conditions.
^(?=.*bjackb)(?=.*bjamesb)(?=.*bjasonb)(?=.*bjulesb).*$
edited Jan 4 at 8:57
answered Dec 8 '10 at 16:17
Alin PurcaruAlin Purcaru
35.5k116385
35.5k116385
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
vim
syntax:^(.*<jack>)@=(.*<james>@=).*$
orv^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to matchblah #tgif blah #friday blah
but^(?=.*bfridayb)(?=.*btgifb).*$
works fine.
– btleffler
Aug 24 '15 at 18:27
1
What doesb
means here?
– VarunAgw
Apr 26 '16 at 12:14
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
|
show 5 more comments
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
vim
syntax:^(.*<jack>)@=(.*<james>@=).*$
orv^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to matchblah #tgif blah #friday blah
but^(?=.*bfridayb)(?=.*btgifb).*$
works fine.
– btleffler
Aug 24 '15 at 18:27
1
What doesb
means here?
– VarunAgw
Apr 26 '16 at 12:14
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
7
7
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
Would somebody mind explaining in a bit more detail how this example works?
– bjmc
Jul 7 '14 at 21:37
1
1
vim
syntax: ^(.*<jack>)@=(.*<james>@=).*$
or v^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
vim
syntax: ^(.*<jack>)@=(.*<james>@=).*$
or v^(.*<jack>)@=(.*<james>)@=.*$
– mykhal
Aug 26 '14 at 15:58
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?
^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to match blah #tgif blah #friday blah
but ^(?=.*bfridayb)(?=.*btgifb).*$
works fine.– btleffler
Aug 24 '15 at 18:27
Does anyone know why this would break (in JavaScript at least) when I try to search for strings starting with '#'?
^(?=.*b#fridayb)(?=.*b#tgifb).*$
fails to match blah #tgif blah #friday blah
but ^(?=.*bfridayb)(?=.*btgifb).*$
works fine.– btleffler
Aug 24 '15 at 18:27
1
1
What does
b
means here?– VarunAgw
Apr 26 '16 at 12:14
What does
b
means here?– VarunAgw
Apr 26 '16 at 12:14
1
1
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
@VarunAgw Word boundary. regular-expressions.info/refwordboundaries.html
– Alin Purcaru
Apr 26 '16 at 14:22
|
show 5 more comments
Try:
james.*jack
If you want both at the same time, then or
them:
james.*jack|jack.*james
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
add a comment |
Try:
james.*jack
If you want both at the same time, then or
them:
james.*jack|jack.*james
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
add a comment |
Try:
james.*jack
If you want both at the same time, then or
them:
james.*jack|jack.*james
Try:
james.*jack
If you want both at the same time, then or
them:
james.*jack|jack.*james
answered Dec 8 '10 at 16:16
icyrock.comicyrock.com
22.4k34973
22.4k34973
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
add a comment |
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
1
1
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
The accepted answer worked. this also worked perfectly for me. For searching code in visual studio 'find results'.
– Yogurt The Wise
May 25 '16 at 13:02
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
This one works for me and is much more concise & easy to understand than the accepted answer!
– Kumar Manish
Sep 26 '17 at 9:20
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
I needed a solution that only had two names to match, so this answer is more concise for that case. But the accepted answer becomes more concise beyond 2 since the number of "or"s increases factorially. For 3 names there would be 6 "or"s, 4 names would be 24 "or"s, etc.
– WileCau
Oct 24 '18 at 0:46
add a comment |
Explanation of command that i am going to write:-
.
means any character, digit can come in place of .
*
means zero or more occurrences of thing written just previous to it.
|
means 'or'.
So,
james.*jack
would search james
, then any number of character until jack
comes.
Since you want either jack.*james
or james.*jack
Hence Command:
jack.*james|james.*jack
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
add a comment |
Explanation of command that i am going to write:-
.
means any character, digit can come in place of .
*
means zero or more occurrences of thing written just previous to it.
|
means 'or'.
So,
james.*jack
would search james
, then any number of character until jack
comes.
Since you want either jack.*james
or james.*jack
Hence Command:
jack.*james|james.*jack
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
add a comment |
Explanation of command that i am going to write:-
.
means any character, digit can come in place of .
*
means zero or more occurrences of thing written just previous to it.
|
means 'or'.
So,
james.*jack
would search james
, then any number of character until jack
comes.
Since you want either jack.*james
or james.*jack
Hence Command:
jack.*james|james.*jack
Explanation of command that i am going to write:-
.
means any character, digit can come in place of .
*
means zero or more occurrences of thing written just previous to it.
|
means 'or'.
So,
james.*jack
would search james
, then any number of character until jack
comes.
Since you want either jack.*james
or james.*jack
Hence Command:
jack.*james|james.*jack
edited Dec 27 '18 at 12:20
Aryeh Beitz
638816
638816
answered Jun 2 '16 at 11:34
Shubham SharmaShubham Sharma
1,078719
1,078719
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
add a comment |
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
3
3
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
As a side note - you could also have edited @icyrock's answer (which is the same as yours, just 6 years earlier), your explanation is very useful on its own.
– WoJ
Jan 23 '18 at 14:24
1
1
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
Thank you for this answer, i however feel the need to point out that in VSCode search, your answer jack.*james | james.*jack will take the spaces between the '|' (or) symbol into consideration during the search. jack.*james|james.*jack works and doesnt look for the spaces
– jgritten
Jun 15 '18 at 17:29
add a comment |
You can do:
bjackb.*bjamesb|bjamesb.*bjackb
add a comment |
You can do:
bjackb.*bjamesb|bjamesb.*bjackb
add a comment |
You can do:
bjackb.*bjamesb|bjamesb.*bjackb
You can do:
bjackb.*bjamesb|bjamesb.*bjackb
answered Dec 8 '10 at 16:16
codaddictcodaddict
347k64438490
347k64438490
add a comment |
add a comment |
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.
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%2f4389644%2fregex-to-match-string-containing-two-names-in-any-order%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
1
Possible duplicate: mulitple-words-in-any-order-using-regex
– Anderson Green
Jun 3 '13 at 4:44