PCRE Regex Match /x… but not /y/x
When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:
https://example.com/x...
But not:
https://example.com/y/x...
To match the /x... we use PCRE regex of:
/x.*
We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:
Match all /x... except /y/x...
Here is what we thought that looked like:
^/(?!y/).x.*
In our mind that reads:
Any query starting with /x..., except starting with /y/x...
Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.
regex pcre
add a comment |
When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:
https://example.com/x...
But not:
https://example.com/y/x...
To match the /x... we use PCRE regex of:
/x.*
We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:
Match all /x... except /y/x...
Here is what we thought that looked like:
^/(?!y/).x.*
In our mind that reads:
Any query starting with /x..., except starting with /y/x...
Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.
regex pcre
add a comment |
When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:
https://example.com/x...
But not:
https://example.com/y/x...
To match the /x... we use PCRE regex of:
/x.*
We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:
Match all /x... except /y/x...
Here is what we thought that looked like:
^/(?!y/).x.*
In our mind that reads:
Any query starting with /x..., except starting with /y/x...
Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.
regex pcre
When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:
https://example.com/x...
But not:
https://example.com/y/x...
To match the /x... we use PCRE regex of:
/x.*
We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:
Match all /x... except /y/x...
Here is what we thought that looked like:
^/(?!y/).x.*
In our mind that reads:
Any query starting with /x..., except starting with /y/x...
Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.
regex pcre
regex pcre
edited Nov 28 '18 at 15:32
beta208
asked Nov 28 '18 at 15:24
beta208beta208
191315
191315
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/
. If that is true, then match any character followed by x and 0+ character. That will match for example //x///
Without taking matching the url part into account, one way could be to use a negative lookahead (?!
to check if what is on the right side does not contain /y/x
and then match any character:
^(?!.*/y/x).+
Regex demo
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
@beta208 That would work^(?!.*/y/x).*/x.*
if there should be/x
in the string. It will match until the last occurrence of/x
and matches any character 0+ times after it.
– The fourth bird
Nov 28 '18 at 15:50
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
add a comment |
You may use a negative lookbehind assertion:
~(?<!/y)/x~
RegEx Demo
(?<!/y)
is a negative lookbehind assertnion that will fail the match if /y
appears before matching /x
.
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.
– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
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%2f53522819%2fpcre-regex-match-x-but-not-y-x%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/
. If that is true, then match any character followed by x and 0+ character. That will match for example //x///
Without taking matching the url part into account, one way could be to use a negative lookahead (?!
to check if what is on the right side does not contain /y/x
and then match any character:
^(?!.*/y/x).+
Regex demo
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
@beta208 That would work^(?!.*/y/x).*/x.*
if there should be/x
in the string. It will match until the last occurrence of/x
and matches any character 0+ times after it.
– The fourth bird
Nov 28 '18 at 15:50
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
add a comment |
Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/
. If that is true, then match any character followed by x and 0+ character. That will match for example //x///
Without taking matching the url part into account, one way could be to use a negative lookahead (?!
to check if what is on the right side does not contain /y/x
and then match any character:
^(?!.*/y/x).+
Regex demo
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
@beta208 That would work^(?!.*/y/x).*/x.*
if there should be/x
in the string. It will match until the last occurrence of/x
and matches any character 0+ times after it.
– The fourth bird
Nov 28 '18 at 15:50
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
add a comment |
Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/
. If that is true, then match any character followed by x and 0+ character. That will match for example //x///
Without taking matching the url part into account, one way could be to use a negative lookahead (?!
to check if what is on the right side does not contain /y/x
and then match any character:
^(?!.*/y/x).+
Regex demo
Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/
. If that is true, then match any character followed by x and 0+ character. That will match for example //x///
Without taking matching the url part into account, one way could be to use a negative lookahead (?!
to check if what is on the right side does not contain /y/x
and then match any character:
^(?!.*/y/x).+
Regex demo
edited Nov 28 '18 at 15:40
answered Nov 28 '18 at 15:30
The fourth birdThe fourth bird
24.2k81629
24.2k81629
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
@beta208 That would work^(?!.*/y/x).*/x.*
if there should be/x
in the string. It will match until the last occurrence of/x
and matches any character 0+ times after it.
– The fourth bird
Nov 28 '18 at 15:50
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
add a comment |
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
@beta208 That would work^(?!.*/y/x).*/x.*
if there should be/x
in the string. It will match until the last occurrence of/x
and matches any character 0+ times after it.
– The fourth bird
Nov 28 '18 at 15:50
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*
– beta208
Nov 28 '18 at 15:35
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...
– beta208
Nov 28 '18 at 15:38
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.
– Poul Bak
Nov 28 '18 at 15:40
1
1
@beta208 That would work
^(?!.*/y/x).*/x.*
if there should be /x
in the string. It will match until the last occurrence of /x
and matches any character 0+ times after it.– The fourth bird
Nov 28 '18 at 15:50
@beta208 That would work
^(?!.*/y/x).*/x.*
if there should be /x
in the string. It will match until the last occurrence of /x
and matches any character 0+ times after it.– The fourth bird
Nov 28 '18 at 15:50
1
1
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
Thank you that does make sense.
– beta208
Nov 28 '18 at 15:53
add a comment |
You may use a negative lookbehind assertion:
~(?<!/y)/x~
RegEx Demo
(?<!/y)
is a negative lookbehind assertnion that will fail the match if /y
appears before matching /x
.
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.
– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
add a comment |
You may use a negative lookbehind assertion:
~(?<!/y)/x~
RegEx Demo
(?<!/y)
is a negative lookbehind assertnion that will fail the match if /y
appears before matching /x
.
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.
– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
add a comment |
You may use a negative lookbehind assertion:
~(?<!/y)/x~
RegEx Demo
(?<!/y)
is a negative lookbehind assertnion that will fail the match if /y
appears before matching /x
.
You may use a negative lookbehind assertion:
~(?<!/y)/x~
RegEx Demo
(?<!/y)
is a negative lookbehind assertnion that will fail the match if /y
appears before matching /x
.
answered Nov 28 '18 at 15:38
anubhavaanubhava
533k48331408
533k48331408
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.
– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
add a comment |
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.
– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
I do see that this also matches and is shorter. I imagine this is the ideal solution?
– beta208
Nov 28 '18 at 15:47
1
1
Yes your requirement needed a lookbehind, moreover this one is more efficient than
^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.– anubhava
Nov 28 '18 at 15:50
Yes your requirement needed a lookbehind, moreover this one is more efficient than
^(?!.*/y/x).*/x
as you can see in no of steps taken in regex demo link I provided.– anubhava
Nov 28 '18 at 15:50
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*
– beta208
Nov 28 '18 at 16:28
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
Sorry didn't understand what you mean by that. What is your actual regex on your system?
– anubhava
Nov 28 '18 at 16:44
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%2f53522819%2fpcre-regex-match-x-but-not-y-x%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