Matching entire, multiline pattern, multiple times but not if match contains stringRegular expression to match a line that doesn't contain a wordRegEx match open tags except XHTML self-contained tagsRegEx - Exclude Matched PatternsNegative lookahead with capturing groupsRegex match a string that doesn't contains a stringRegular expression, match anything but these stringsConditional Regex for multiple matches in a lineHow to match string followed by repeated pattern in regex?Regex negative lookahead for url parameterregex look ahead behind (look around) negative problems
Why does Intel's Haswell chip allow FP multiplication to be twice as fast as addition?
Simple Stop watch which i want to extend
What game uses dice with sides powers of 2?
Best gun to modify into a monsterhunter weapon?
Wherein the Shatapatha Brahmana it was mentioned about 8.64 lakh alphabets in Vedas?
Who are these characters/superheroes in the posters from Chris's room in Family Guy?
How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?
Non-OR journals which regularly publish OR research
What does "sardine box" mean?
How to avoid the "need" to learn more before conducting research?
Word or idiom defining something barely functional
How to create all combinations from a nested list while preserving the structure using R?
Is TA-ing worth the opportunity cost?
Should you play baroque pieces a semitone lower?
Is Texas Instrument wrong with their pin number on TO-92 package?
Write an interpreter for *
What costs less energy? Roll or Yaw?
Why are Gatwick's runways too close together?
Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout
Replace value with variable length between double quotes
Does this Foo machine halt?
Identification of vintage sloping window
What is the difference between 型 and 形?
Should RL rewards diminish over time?
Matching entire, multiline pattern, multiple times but not if match contains string
Regular expression to match a line that doesn't contain a wordRegEx match open tags except XHTML self-contained tagsRegEx - Exclude Matched PatternsNegative lookahead with capturing groupsRegex match a string that doesn't contains a stringRegular expression, match anything but these stringsConditional Regex for multiple matches in a lineHow to match string followed by repeated pattern in regex?Regex negative lookahead for url parameterregex look ahead behind (look around) negative problems
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Imagine I have this text (I realise using regex to parse HTML is not the correct solution...)
<div><p>HELLO1</p>
<span>SPIDER</span></div>
<div><p>HELLO2</p>
<span>CHEESE</span></div>
<div><p>HELLO3</p>
<span>BANANA</span></div>
I want to match text inside the <p> and <span> respectively (this is a contrived example)
I can achieve that by using:
/<p>(.*?)</p>.*?<span>(.*?)</span>.*?</div>/gsmi
However, I don't want to match if the text inside the span is CHEESE.
Ive tried to use a negative lookahead like so:
/<p>(.*?)</p>.*?<span>((?!CHEESE).*?)</span>.*?</div>/gsmi
However, that now matches the wrong thing as the pattern matches to HELLO2 and BANANA where I want HELLO3 and BANANA
I realise this is because Im using .*? but this is required for the real life solution.
Example here: https://regex101.com/r/h4YgDm/3
How can I match, a whole pattern only but still spanning multiple lines?
regex regex-negation regex-lookarounds
add a comment |
Imagine I have this text (I realise using regex to parse HTML is not the correct solution...)
<div><p>HELLO1</p>
<span>SPIDER</span></div>
<div><p>HELLO2</p>
<span>CHEESE</span></div>
<div><p>HELLO3</p>
<span>BANANA</span></div>
I want to match text inside the <p> and <span> respectively (this is a contrived example)
I can achieve that by using:
/<p>(.*?)</p>.*?<span>(.*?)</span>.*?</div>/gsmi
However, I don't want to match if the text inside the span is CHEESE.
Ive tried to use a negative lookahead like so:
/<p>(.*?)</p>.*?<span>((?!CHEESE).*?)</span>.*?</div>/gsmi
However, that now matches the wrong thing as the pattern matches to HELLO2 and BANANA where I want HELLO3 and BANANA
I realise this is because Im using .*? but this is required for the real life solution.
Example here: https://regex101.com/r/h4YgDm/3
How can I match, a whole pattern only but still spanning multiple lines?
regex regex-negation regex-lookarounds
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28
add a comment |
Imagine I have this text (I realise using regex to parse HTML is not the correct solution...)
<div><p>HELLO1</p>
<span>SPIDER</span></div>
<div><p>HELLO2</p>
<span>CHEESE</span></div>
<div><p>HELLO3</p>
<span>BANANA</span></div>
I want to match text inside the <p> and <span> respectively (this is a contrived example)
I can achieve that by using:
/<p>(.*?)</p>.*?<span>(.*?)</span>.*?</div>/gsmi
However, I don't want to match if the text inside the span is CHEESE.
Ive tried to use a negative lookahead like so:
/<p>(.*?)</p>.*?<span>((?!CHEESE).*?)</span>.*?</div>/gsmi
However, that now matches the wrong thing as the pattern matches to HELLO2 and BANANA where I want HELLO3 and BANANA
I realise this is because Im using .*? but this is required for the real life solution.
Example here: https://regex101.com/r/h4YgDm/3
How can I match, a whole pattern only but still spanning multiple lines?
regex regex-negation regex-lookarounds
Imagine I have this text (I realise using regex to parse HTML is not the correct solution...)
<div><p>HELLO1</p>
<span>SPIDER</span></div>
<div><p>HELLO2</p>
<span>CHEESE</span></div>
<div><p>HELLO3</p>
<span>BANANA</span></div>
I want to match text inside the <p> and <span> respectively (this is a contrived example)
I can achieve that by using:
/<p>(.*?)</p>.*?<span>(.*?)</span>.*?</div>/gsmi
However, I don't want to match if the text inside the span is CHEESE.
Ive tried to use a negative lookahead like so:
/<p>(.*?)</p>.*?<span>((?!CHEESE).*?)</span>.*?</div>/gsmi
However, that now matches the wrong thing as the pattern matches to HELLO2 and BANANA where I want HELLO3 and BANANA
I realise this is because Im using .*? but this is required for the real life solution.
Example here: https://regex101.com/r/h4YgDm/3
How can I match, a whole pattern only but still spanning multiple lines?
regex regex-negation regex-lookarounds
regex regex-negation regex-lookarounds
edited Mar 27 at 8:34
Andrew Hall
asked Mar 27 at 8:23
Andrew HallAndrew Hall
2,75415 silver badges29 bronze badges
2,75415 silver badges29 bronze badges
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28
add a comment |
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28
add a comment |
2 Answers
2
active
oldest
votes
You could achieve what you want in two steps. First, extract the div elements which meet the criteria (ie. no CHEESE) like this (demo):
<div>(?:(?!CHEESE).)*?</div>
Second, select the content between p and span tags as two groups (demo):
(?<=<p>)(.*?)(?=</p>)(?:.*?)(?<=<span>)(.*?)(?=</span>)
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
add a comment |
A colleague of mine sent me this answer, which does what I want:
https://regex101.com/r/h4YgDm/8
Regex: (?:(?:<div><p>(w*)</p>s)(?!<span>CHEESE</span>)(?:<span>(w*)</span>)</div>)
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%2f55372631%2fmatching-entire-multiline-pattern-multiple-times-but-not-if-match-contains-str%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
You could achieve what you want in two steps. First, extract the div elements which meet the criteria (ie. no CHEESE) like this (demo):
<div>(?:(?!CHEESE).)*?</div>
Second, select the content between p and span tags as two groups (demo):
(?<=<p>)(.*?)(?=</p>)(?:.*?)(?<=<span>)(.*?)(?=</span>)
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
add a comment |
You could achieve what you want in two steps. First, extract the div elements which meet the criteria (ie. no CHEESE) like this (demo):
<div>(?:(?!CHEESE).)*?</div>
Second, select the content between p and span tags as two groups (demo):
(?<=<p>)(.*?)(?=</p>)(?:.*?)(?<=<span>)(.*?)(?=</span>)
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
add a comment |
You could achieve what you want in two steps. First, extract the div elements which meet the criteria (ie. no CHEESE) like this (demo):
<div>(?:(?!CHEESE).)*?</div>
Second, select the content between p and span tags as two groups (demo):
(?<=<p>)(.*?)(?=</p>)(?:.*?)(?<=<span>)(.*?)(?=</span>)
You could achieve what you want in two steps. First, extract the div elements which meet the criteria (ie. no CHEESE) like this (demo):
<div>(?:(?!CHEESE).)*?</div>
Second, select the content between p and span tags as two groups (demo):
(?<=<p>)(.*?)(?=</p>)(?:.*?)(?<=<span>)(.*?)(?=</span>)
answered Mar 27 at 13:27
glhrglhr
3,3231 gold badge9 silver badges21 bronze badges
3,3231 gold badge9 silver badges21 bronze badges
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
add a comment |
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Is there anyway this can done in one step? Perhaps using back references?
– Andrew Hall
Mar 27 at 14:29
Not as far as I know.
– glhr
Mar 27 at 15:15
Not as far as I know.
– glhr
Mar 27 at 15:15
add a comment |
A colleague of mine sent me this answer, which does what I want:
https://regex101.com/r/h4YgDm/8
Regex: (?:(?:<div><p>(w*)</p>s)(?!<span>CHEESE</span>)(?:<span>(w*)</span>)</div>)
add a comment |
A colleague of mine sent me this answer, which does what I want:
https://regex101.com/r/h4YgDm/8
Regex: (?:(?:<div><p>(w*)</p>s)(?!<span>CHEESE</span>)(?:<span>(w*)</span>)</div>)
add a comment |
A colleague of mine sent me this answer, which does what I want:
https://regex101.com/r/h4YgDm/8
Regex: (?:(?:<div><p>(w*)</p>s)(?!<span>CHEESE</span>)(?:<span>(w*)</span>)</div>)
A colleague of mine sent me this answer, which does what I want:
https://regex101.com/r/h4YgDm/8
Regex: (?:(?:<div><p>(w*)</p>s)(?!<span>CHEESE</span>)(?:<span>(w*)</span>)</div>)
answered Mar 27 at 18:06
Andrew HallAndrew Hall
2,75415 silver badges29 bronze badges
2,75415 silver badges29 bronze badges
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%2f55372631%2fmatching-entire-multiline-pattern-multiple-times-but-not-if-match-contains-str%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
You have no <p> tag in your example.
– Reymart Betana
Mar 27 at 8:26
@ReymartBetana Thanks - I refactored example, whilst writing
– Andrew Hall
Mar 27 at 8:28