Regex Match a string that is indirectly bound by 2 strings
Here is my abstracted test string:
test test test
blue
test test test
red 23
test test test
grey
test test test
blue
test test test
red 99
test test test
grey - white
test test test
I am trying to match the text between the second "blue" and "grey - white"
Basically I'm parsing some code, and need 2 rules:
1.) Find red if between "blue" and "grey"
2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas
The first one isn't too hard: Link
But I can't figure out the second rule. Everything I tried...
like (?s)(blue)(.*?)(grey(?!s+test))
... still matches the first "blue", instead of skipping over it
Is there away to turn (.*?) into 'but only if does not contain "blue" '
Anyone know a trick I do not?
regex regex-lookarounds
add a comment |
Here is my abstracted test string:
test test test
blue
test test test
red 23
test test test
grey
test test test
blue
test test test
red 99
test test test
grey - white
test test test
I am trying to match the text between the second "blue" and "grey - white"
Basically I'm parsing some code, and need 2 rules:
1.) Find red if between "blue" and "grey"
2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas
The first one isn't too hard: Link
But I can't figure out the second rule. Everything I tried...
like (?s)(blue)(.*?)(grey(?!s+test))
... still matches the first "blue", instead of skipping over it
Is there away to turn (.*?) into 'but only if does not contain "blue" '
Anyone know a trick I do not?
regex regex-lookarounds
add a comment |
Here is my abstracted test string:
test test test
blue
test test test
red 23
test test test
grey
test test test
blue
test test test
red 99
test test test
grey - white
test test test
I am trying to match the text between the second "blue" and "grey - white"
Basically I'm parsing some code, and need 2 rules:
1.) Find red if between "blue" and "grey"
2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas
The first one isn't too hard: Link
But I can't figure out the second rule. Everything I tried...
like (?s)(blue)(.*?)(grey(?!s+test))
... still matches the first "blue", instead of skipping over it
Is there away to turn (.*?) into 'but only if does not contain "blue" '
Anyone know a trick I do not?
regex regex-lookarounds
Here is my abstracted test string:
test test test
blue
test test test
red 23
test test test
grey
test test test
blue
test test test
red 99
test test test
grey - white
test test test
I am trying to match the text between the second "blue" and "grey - white"
Basically I'm parsing some code, and need 2 rules:
1.) Find red if between "blue" and "grey"
2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas
The first one isn't too hard: Link
But I can't figure out the second rule. Everything I tried...
like (?s)(blue)(.*?)(grey(?!s+test))
... still matches the first "blue", instead of skipping over it
Is there away to turn (.*?) into 'but only if does not contain "blue" '
Anyone know a trick I do not?
regex regex-lookarounds
regex regex-lookarounds
asked Jan 2 at 16:31
NorsakNorsak
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I believe you are looking for this based on your 2 rules although they don't match your regex. regex101
(blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)
Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.
add a comment |
(?s)(blue).*?(red).*?(grey(?: - blue)?)
The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)
.
Let me re-write the question to clarify the objective:
I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):
1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)
2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
add a comment |
This regex (?s)(blue)(.*?)(grey(?!s+test))
will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test))
which does match in the middle and fails the negative lookahead.
What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"
(?s)(blue).*(red d+).*?(grey - white)
See the Regex demo
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%2f54009870%2fregex-match-a-string-that-is-indirectly-bound-by-2-strings%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
I believe you are looking for this based on your 2 rules although they don't match your regex. regex101
(blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)
Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.
add a comment |
I believe you are looking for this based on your 2 rules although they don't match your regex. regex101
(blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)
Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.
add a comment |
I believe you are looking for this based on your 2 rules although they don't match your regex. regex101
(blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)
Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.
I believe you are looking for this based on your 2 rules although they don't match your regex. regex101
(blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)
Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.
answered Jan 2 at 18:25
Gary_WGary_W
6,99811129
6,99811129
add a comment |
add a comment |
(?s)(blue).*?(red).*?(grey(?: - blue)?)
The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)
.
Let me re-write the question to clarify the objective:
I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):
1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)
2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
add a comment |
(?s)(blue).*?(red).*?(grey(?: - blue)?)
The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)
.
Let me re-write the question to clarify the objective:
I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):
1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)
2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
add a comment |
(?s)(blue).*?(red).*?(grey(?: - blue)?)
The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)
.
Let me re-write the question to clarify the objective:
I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):
1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)
2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)
(?s)(blue).*?(red).*?(grey(?: - blue)?)
The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)
.
Let me re-write the question to clarify the objective:
I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):
1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)
2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)
edited Jan 2 at 23:31
answered Jan 2 at 22:09


NorsakNorsak
44
44
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
add a comment |
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.
– The fourth bird
Jan 3 at 7:50
add a comment |
This regex (?s)(blue)(.*?)(grey(?!s+test))
will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test))
which does match in the middle and fails the negative lookahead.
What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"
(?s)(blue).*(red d+).*?(grey - white)
See the Regex demo
add a comment |
This regex (?s)(blue)(.*?)(grey(?!s+test))
will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test))
which does match in the middle and fails the negative lookahead.
What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"
(?s)(blue).*(red d+).*?(grey - white)
See the Regex demo
add a comment |
This regex (?s)(blue)(.*?)(grey(?!s+test))
will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test))
which does match in the middle and fails the negative lookahead.
What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"
(?s)(blue).*(red d+).*?(grey - white)
See the Regex demo
This regex (?s)(blue)(.*?)(grey(?!s+test))
will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test))
which does match in the middle and fails the negative lookahead.
What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"
(?s)(blue).*(red d+).*?(grey - white)
See the Regex demo
edited Jan 3 at 7:48
answered Jan 2 at 19:20
The fourth birdThe fourth bird
24.3k81629
24.3k81629
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%2f54009870%2fregex-match-a-string-that-is-indirectly-bound-by-2-strings%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