php regex: Alternative to backreference in negative lookbehind
I want to find instances where a captured group does not appear later in the string:
aaaBbb = CccBbb <- format is valid, skip
aaaDddd = CccDddd <- format is valid, skip
aaaEeee = CccFfff <- format is not valid, match this one only
So this matches the lines I don't want to match ( https://regex101.com/r/lon87L/1 )
/^ +w+([A-Z][a-z+]) += +w+1$/mg
I've read on https://www.regular-expressions.info/refadv.html that php doesn't support backreferences inside a negative lookbehind, but other implementations of regex can. So something like this would match the invalid lines that I want to match, but it doesn't work in php:
/^ +w+([A-Z][a-z+]) += +w+(?<!1)$/mg
Is there anything else that would work, other than matching all of three lines and looping through the matches in a php foreach?
php regex negative-lookbehind
add a comment |
I want to find instances where a captured group does not appear later in the string:
aaaBbb = CccBbb <- format is valid, skip
aaaDddd = CccDddd <- format is valid, skip
aaaEeee = CccFfff <- format is not valid, match this one only
So this matches the lines I don't want to match ( https://regex101.com/r/lon87L/1 )
/^ +w+([A-Z][a-z+]) += +w+1$/mg
I've read on https://www.regular-expressions.info/refadv.html that php doesn't support backreferences inside a negative lookbehind, but other implementations of regex can. So something like this would match the invalid lines that I want to match, but it doesn't work in php:
/^ +w+([A-Z][a-z+]) += +w+(?<!1)$/mg
Is there anything else that would work, other than matching all of three lines and looping through the matches in a php foreach?
php regex negative-lookbehind
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.
– sln
Nov 15 '18 at 22:19
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
If it has to be at the EOS, just add a$
after the backref regex101.com/r/QuXJLY/1
– sln
Nov 15 '18 at 22:27
add a comment |
I want to find instances where a captured group does not appear later in the string:
aaaBbb = CccBbb <- format is valid, skip
aaaDddd = CccDddd <- format is valid, skip
aaaEeee = CccFfff <- format is not valid, match this one only
So this matches the lines I don't want to match ( https://regex101.com/r/lon87L/1 )
/^ +w+([A-Z][a-z+]) += +w+1$/mg
I've read on https://www.regular-expressions.info/refadv.html that php doesn't support backreferences inside a negative lookbehind, but other implementations of regex can. So something like this would match the invalid lines that I want to match, but it doesn't work in php:
/^ +w+([A-Z][a-z+]) += +w+(?<!1)$/mg
Is there anything else that would work, other than matching all of three lines and looping through the matches in a php foreach?
php regex negative-lookbehind
I want to find instances where a captured group does not appear later in the string:
aaaBbb = CccBbb <- format is valid, skip
aaaDddd = CccDddd <- format is valid, skip
aaaEeee = CccFfff <- format is not valid, match this one only
So this matches the lines I don't want to match ( https://regex101.com/r/lon87L/1 )
/^ +w+([A-Z][a-z+]) += +w+1$/mg
I've read on https://www.regular-expressions.info/refadv.html that php doesn't support backreferences inside a negative lookbehind, but other implementations of regex can. So something like this would match the invalid lines that I want to match, but it doesn't work in php:
/^ +w+([A-Z][a-z+]) += +w+(?<!1)$/mg
Is there anything else that would work, other than matching all of three lines and looping through the matches in a php foreach?
php regex negative-lookbehind
php regex negative-lookbehind
asked Nov 15 '18 at 22:00
RedzarfRedzarf
1,3261732
1,3261732
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.
– sln
Nov 15 '18 at 22:19
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
If it has to be at the EOS, just add a$
after the backref regex101.com/r/QuXJLY/1
– sln
Nov 15 '18 at 22:27
add a comment |
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.
– sln
Nov 15 '18 at 22:19
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
If it has to be at the EOS, just add a$
after the backref regex101.com/r/QuXJLY/1
– sln
Nov 15 '18 at 22:27
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to
(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.– sln
Nov 15 '18 at 22:19
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to
(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.– sln
Nov 15 '18 at 22:19
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
If it has to be at the EOS, just add a
$
after the backref regex101.com/r/QuXJLY/1– sln
Nov 15 '18 at 22:27
If it has to be at the EOS, just add a
$
after the backref regex101.com/r/QuXJLY/1– sln
Nov 15 '18 at 22:27
add a comment |
2 Answers
2
active
oldest
votes
Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.
^ +w+([A-Z][a-z]+) += +(?!w+1).*$
regex101 demo
PHP demo
add a comment |
One option would be to, right before each repeated w
after the =
, use negative lookahead for 1$
:
^ +w+([A-Z][a-z]+) += +(?:(?!1$)w)+$
^^^^^^^^^^^^^^
https://regex101.com/r/lon87L/2
But that only excludes a match if the backreference occurs right at the end of the string. If you want to ensure that the previously matched phrase doesn't occur anywhere within the final w
s, just remove the $
from inside the repeated group:
^ +w+([A-Z][a-z]+) += +(?:(?!1)w)+$
^
https://regex101.com/r/lon87L/3
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%2f53328505%2fphp-regex-alternative-to-backreference-in-negative-lookbehind%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
Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.
^ +w+([A-Z][a-z]+) += +(?!w+1).*$
regex101 demo
PHP demo
add a comment |
Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.
^ +w+([A-Z][a-z]+) += +(?!w+1).*$
regex101 demo
PHP demo
add a comment |
Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.
^ +w+([A-Z][a-z]+) += +(?!w+1).*$
regex101 demo
PHP demo
Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.
^ +w+([A-Z][a-z]+) += +(?!w+1).*$
regex101 demo
PHP demo
edited Nov 15 '18 at 22:45
answered Nov 15 '18 at 22:29
DavіdDavіd
3,66041635
3,66041635
add a comment |
add a comment |
One option would be to, right before each repeated w
after the =
, use negative lookahead for 1$
:
^ +w+([A-Z][a-z]+) += +(?:(?!1$)w)+$
^^^^^^^^^^^^^^
https://regex101.com/r/lon87L/2
But that only excludes a match if the backreference occurs right at the end of the string. If you want to ensure that the previously matched phrase doesn't occur anywhere within the final w
s, just remove the $
from inside the repeated group:
^ +w+([A-Z][a-z]+) += +(?:(?!1)w)+$
^
https://regex101.com/r/lon87L/3
add a comment |
One option would be to, right before each repeated w
after the =
, use negative lookahead for 1$
:
^ +w+([A-Z][a-z]+) += +(?:(?!1$)w)+$
^^^^^^^^^^^^^^
https://regex101.com/r/lon87L/2
But that only excludes a match if the backreference occurs right at the end of the string. If you want to ensure that the previously matched phrase doesn't occur anywhere within the final w
s, just remove the $
from inside the repeated group:
^ +w+([A-Z][a-z]+) += +(?:(?!1)w)+$
^
https://regex101.com/r/lon87L/3
add a comment |
One option would be to, right before each repeated w
after the =
, use negative lookahead for 1$
:
^ +w+([A-Z][a-z]+) += +(?:(?!1$)w)+$
^^^^^^^^^^^^^^
https://regex101.com/r/lon87L/2
But that only excludes a match if the backreference occurs right at the end of the string. If you want to ensure that the previously matched phrase doesn't occur anywhere within the final w
s, just remove the $
from inside the repeated group:
^ +w+([A-Z][a-z]+) += +(?:(?!1)w)+$
^
https://regex101.com/r/lon87L/3
One option would be to, right before each repeated w
after the =
, use negative lookahead for 1$
:
^ +w+([A-Z][a-z]+) += +(?:(?!1$)w)+$
^^^^^^^^^^^^^^
https://regex101.com/r/lon87L/2
But that only excludes a match if the backreference occurs right at the end of the string. If you want to ensure that the previously matched phrase doesn't occur anywhere within the final w
s, just remove the $
from inside the repeated group:
^ +w+([A-Z][a-z]+) += +(?:(?!1)w)+$
^
https://regex101.com/r/lon87L/3
answered Nov 15 '18 at 22:07
CertainPerformanceCertainPerformance
82.6k144067
82.6k144067
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%2f53328505%2fphp-regex-alternative-to-backreference-in-negative-lookbehind%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
Negative lookbehinds require a compile time fixed length. A backreference is a runtime item with variable length. One option is to
(?>1(*SKIP)(*FAIL)|w)+
and match the backreference. This is probably quicker too.– sln
Nov 15 '18 at 22:19
You can see it here regex101.com/r/6gfSBi/1 Btw, only the Dot-Net engine supports variable width lookbehinds (including backreferences).
– sln
Nov 15 '18 at 22:22
If it has to be at the EOS, just add a
$
after the backref regex101.com/r/QuXJLY/1– sln
Nov 15 '18 at 22:27