regular expression to find lines containing multiple specific words or patterns in any arbitrary orderUsing a variable in a regex patternHow to replace specific lines with regex?Delete all of a file except for certain words that contain certain lettersHow to replace content between two patterns from the file?Find a non-regular expression tag in a specific tags fileSubstitute words enclosed between comma or open-parenthesis and comma or closed parenthesisHow to add quantifiers to group in a pattern in VIM?VIM regex - match all words equal to one under cursor, except one currently under the cursorreplace lines with the first and the last wordSwap grid[X][Y] to grid[Y][X] in full file
Simple Arithmetic Puzzle 7. Or is it?
Hotel booking: Why is Agoda much cheaper than booking.com?
List of lists elementwise greater/smaller than
How should I mix small caps with digits or symbols?
US F1 Visa grace period attending a conference
Don't understand notation of morphisms in Monoid definition
How to safely discharge oneself
tikz: 5 squares on a row, roman numbered 1 -> 5
Was murdering a slave illegal in American slavery, and if so, what punishments were given for it?
Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?
How did Arya and the Hound get into King's Landing so easily?
How to prove the emptiness of intersection of two context free languages is undecidable?
Warped chessboard
Farthing / Riding
Eigenvalues of the Laplace-Beltrami operator on a compact Riemannnian manifold
If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?
How to add a low pass filter to this non-inverting amplifier circuit?
What city and town structures are important in a low fantasy medieval world?
pwaS eht tirsf dna tasl setterl fo hace dorw
Why is this python script running in background consuming 100 % CPU?
Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?
Best practice for printing and evaluating formulas with the minimal coding
How can I use 400 ASA film in a Leica IIIf, which does not have options higher than 100?
Existence of a model of ZFC in which the natural numbers are really the natural numbers
regular expression to find lines containing multiple specific words or patterns in any arbitrary order
Using a variable in a regex patternHow to replace specific lines with regex?Delete all of a file except for certain words that contain certain lettersHow to replace content between two patterns from the file?Find a non-regular expression tag in a specific tags fileSubstitute words enclosed between comma or open-parenthesis and comma or closed parenthesisHow to add quantifiers to group in a pattern in VIM?VIM regex - match all words equal to one under cursor, except one currently under the cursorreplace lines with the first and the last wordSwap grid[X][Y] to grid[Y][X] in full file
Suppose we have the following very simple file:
1 x 3 x
x 3 x 1
Now I'd like to have a pattern that matches all lines containing both 1
and 3
. The order and position should not matter.
(Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)
regular-expression
New contributor
add a comment |
Suppose we have the following very simple file:
1 x 3 x
x 3 x 1
Now I'd like to have a pattern that matches all lines containing both 1
and 3
. The order and position should not matter.
(Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)
regular-expression
New contributor
add a comment |
Suppose we have the following very simple file:
1 x 3 x
x 3 x 1
Now I'd like to have a pattern that matches all lines containing both 1
and 3
. The order and position should not matter.
(Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)
regular-expression
New contributor
Suppose we have the following very simple file:
1 x 3 x
x 3 x 1
Now I'd like to have a pattern that matches all lines containing both 1
and 3
. The order and position should not matter.
(Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)
regular-expression
regular-expression
New contributor
New contributor
New contributor
asked May 14 at 7:26
myrddmyrdd
1636
1636
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Another way is to use :h /& , it works a bit like &&
in most coding language:
pattern0&pattern1
Above expression will match pattern1 if pattern0 also match at the same position.
To solve your example:
v^(.*1&.*3).*$
v
very magic^
start of line(.*1&.*3)
match.*3
if.*1
matches..*$
match anything until line end
If you have multiple patterns, you can write it as:
v^(.*pattern0&.*pattern1&.*pattern2...).*$
2
I've never really understood what/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the:help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1
– Rich
May 14 at 9:52
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
it works a bit like && in most coding language:
is brilliant!
– Maxim Kim
May 15 at 8:20
add a comment |
Here's another alternative: use :h /bar
to match either …1…3… or …3…1…:
v.*(1.*3|3.*1).*
v
very magic.*
anything (or nothing)(...)
a group, containing either:1.*3
1
followed by anything followed by3
|
or3.*1
3
followed by anything followed by1
.*
anything
nicely explained, therefore +1
– myrdd
May 15 at 8:12
add a comment |
One option is to use a pattern with multiple lookaheads:
magic: ^(.*1)@=(.*3)@=
‾ ‾
very magic: v^(.*1)@=(.*3)@=
‾ ‾
Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.
additional hint:
This even allows one to add another condition to the regular expression: matching lines that contain 1
and 3
, but do not contain x
:
magic: ^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
very magic: v^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
New contributor
1
I'd consider removing the<
,>
,<
, and>
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!
– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
myrdd is a new contributor. Be nice, and check out our Code of Conduct.
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%2fvi.stackexchange.com%2fquestions%2f19945%2fregular-expression-to-find-lines-containing-multiple-specific-words-or-patterns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Another way is to use :h /& , it works a bit like &&
in most coding language:
pattern0&pattern1
Above expression will match pattern1 if pattern0 also match at the same position.
To solve your example:
v^(.*1&.*3).*$
v
very magic^
start of line(.*1&.*3)
match.*3
if.*1
matches..*$
match anything until line end
If you have multiple patterns, you can write it as:
v^(.*pattern0&.*pattern1&.*pattern2...).*$
2
I've never really understood what/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the:help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1
– Rich
May 14 at 9:52
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
it works a bit like && in most coding language:
is brilliant!
– Maxim Kim
May 15 at 8:20
add a comment |
Another way is to use :h /& , it works a bit like &&
in most coding language:
pattern0&pattern1
Above expression will match pattern1 if pattern0 also match at the same position.
To solve your example:
v^(.*1&.*3).*$
v
very magic^
start of line(.*1&.*3)
match.*3
if.*1
matches..*$
match anything until line end
If you have multiple patterns, you can write it as:
v^(.*pattern0&.*pattern1&.*pattern2...).*$
2
I've never really understood what/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the:help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1
– Rich
May 14 at 9:52
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
it works a bit like && in most coding language:
is brilliant!
– Maxim Kim
May 15 at 8:20
add a comment |
Another way is to use :h /& , it works a bit like &&
in most coding language:
pattern0&pattern1
Above expression will match pattern1 if pattern0 also match at the same position.
To solve your example:
v^(.*1&.*3).*$
v
very magic^
start of line(.*1&.*3)
match.*3
if.*1
matches..*$
match anything until line end
If you have multiple patterns, you can write it as:
v^(.*pattern0&.*pattern1&.*pattern2...).*$
Another way is to use :h /& , it works a bit like &&
in most coding language:
pattern0&pattern1
Above expression will match pattern1 if pattern0 also match at the same position.
To solve your example:
v^(.*1&.*3).*$
v
very magic^
start of line(.*1&.*3)
match.*3
if.*1
matches..*$
match anything until line end
If you have multiple patterns, you can write it as:
v^(.*pattern0&.*pattern1&.*pattern2...).*$
edited May 14 at 8:14
answered May 14 at 8:05
dedowsdidedowsdi
1,182411
1,182411
2
I've never really understood what/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the:help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1
– Rich
May 14 at 9:52
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
it works a bit like && in most coding language:
is brilliant!
– Maxim Kim
May 15 at 8:20
add a comment |
2
I've never really understood what/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the:help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1
– Rich
May 14 at 9:52
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
it works a bit like && in most coding language:
is brilliant!
– Maxim Kim
May 15 at 8:20
2
2
I've never really understood what
/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1– Rich
May 14 at 9:52
I've never really understood what
/&
is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help
. Still, reading your answer, it finally clicked in my brain. Thanks! +1– Rich
May 14 at 9:52
3
3
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.
– D. Ben Knoble
May 14 at 17:11
2
2
it works a bit like && in most coding language:
is brilliant!– Maxim Kim
May 15 at 8:20
it works a bit like && in most coding language:
is brilliant!– Maxim Kim
May 15 at 8:20
add a comment |
Here's another alternative: use :h /bar
to match either …1…3… or …3…1…:
v.*(1.*3|3.*1).*
v
very magic.*
anything (or nothing)(...)
a group, containing either:1.*3
1
followed by anything followed by3
|
or3.*1
3
followed by anything followed by1
.*
anything
nicely explained, therefore +1
– myrdd
May 15 at 8:12
add a comment |
Here's another alternative: use :h /bar
to match either …1…3… or …3…1…:
v.*(1.*3|3.*1).*
v
very magic.*
anything (or nothing)(...)
a group, containing either:1.*3
1
followed by anything followed by3
|
or3.*1
3
followed by anything followed by1
.*
anything
nicely explained, therefore +1
– myrdd
May 15 at 8:12
add a comment |
Here's another alternative: use :h /bar
to match either …1…3… or …3…1…:
v.*(1.*3|3.*1).*
v
very magic.*
anything (or nothing)(...)
a group, containing either:1.*3
1
followed by anything followed by3
|
or3.*1
3
followed by anything followed by1
.*
anything
Here's another alternative: use :h /bar
to match either …1…3… or …3…1…:
v.*(1.*3|3.*1).*
v
very magic.*
anything (or nothing)(...)
a group, containing either:1.*3
1
followed by anything followed by3
|
or3.*1
3
followed by anything followed by1
.*
anything
edited May 14 at 9:47
answered May 14 at 9:41
RichRich
15.9k12066
15.9k12066
nicely explained, therefore +1
– myrdd
May 15 at 8:12
add a comment |
nicely explained, therefore +1
– myrdd
May 15 at 8:12
nicely explained, therefore +1
– myrdd
May 15 at 8:12
nicely explained, therefore +1
– myrdd
May 15 at 8:12
add a comment |
One option is to use a pattern with multiple lookaheads:
magic: ^(.*1)@=(.*3)@=
‾ ‾
very magic: v^(.*1)@=(.*3)@=
‾ ‾
Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.
additional hint:
This even allows one to add another condition to the regular expression: matching lines that contain 1
and 3
, but do not contain x
:
magic: ^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
very magic: v^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
New contributor
1
I'd consider removing the<
,>
,<
, and>
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!
– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
add a comment |
One option is to use a pattern with multiple lookaheads:
magic: ^(.*1)@=(.*3)@=
‾ ‾
very magic: v^(.*1)@=(.*3)@=
‾ ‾
Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.
additional hint:
This even allows one to add another condition to the regular expression: matching lines that contain 1
and 3
, but do not contain x
:
magic: ^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
very magic: v^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
New contributor
1
I'd consider removing the<
,>
,<
, and>
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!
– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
add a comment |
One option is to use a pattern with multiple lookaheads:
magic: ^(.*1)@=(.*3)@=
‾ ‾
very magic: v^(.*1)@=(.*3)@=
‾ ‾
Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.
additional hint:
This even allows one to add another condition to the regular expression: matching lines that contain 1
and 3
, but do not contain x
:
magic: ^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
very magic: v^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
New contributor
One option is to use a pattern with multiple lookaheads:
magic: ^(.*1)@=(.*3)@=
‾ ‾
very magic: v^(.*1)@=(.*3)@=
‾ ‾
Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.
additional hint:
This even allows one to add another condition to the regular expression: matching lines that contain 1
and 3
, but do not contain x
:
magic: ^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
very magic: v^(.*1)@=(.*3)@=(.*x)@!
‾ ‾ ‾
New contributor
edited yesterday
New contributor
answered May 14 at 7:26
myrddmyrdd
1636
1636
New contributor
New contributor
1
I'd consider removing the<
,>
,<
, and>
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!
– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
add a comment |
1
I'd consider removing the<
,>
,<
, and>
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!
– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
1
1
I'd consider removing the
<
, >
, <
, and >
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!– Rich
2 days ago
I'd consider removing the
<
, >
, <
, and >
end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!– Rich
2 days ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
thank you @Rich for your edit and your suggestion!
– myrdd
21 hours ago
add a comment |
myrdd is a new contributor. Be nice, and check out our Code of Conduct.
myrdd is a new contributor. Be nice, and check out our Code of Conduct.
myrdd is a new contributor. Be nice, and check out our Code of Conduct.
myrdd is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Vi and Vim Stack Exchange!
- 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%2fvi.stackexchange.com%2fquestions%2f19945%2fregular-expression-to-find-lines-containing-multiple-specific-words-or-patterns%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