Regex match the keyword everywhere except inside a URL












-4















How can I create a regex that will match the keyword "politics" everywhere except for in the URL below? I am trying to use a negative lookahead.



The string is a paragraph of text that looks like this:




politics are good.
http://www.foxnews.com/politics/2018/09/07/omarosa-plans-another-tape-release-next-week-report.html

politics are bad.




I want to the 1st and 3rd matches, but not the second, which is part of a URL.










share|improve this question

























  • What is your regex tool?

    – anubhava
    Nov 15 '18 at 18:49











  • The stack programming seems to be raising to next level, where people tend to ask for solution without trying

    – PeterM
    Nov 15 '18 at 19:49


















-4















How can I create a regex that will match the keyword "politics" everywhere except for in the URL below? I am trying to use a negative lookahead.



The string is a paragraph of text that looks like this:




politics are good.
http://www.foxnews.com/politics/2018/09/07/omarosa-plans-another-tape-release-next-week-report.html

politics are bad.




I want to the 1st and 3rd matches, but not the second, which is part of a URL.










share|improve this question

























  • What is your regex tool?

    – anubhava
    Nov 15 '18 at 18:49











  • The stack programming seems to be raising to next level, where people tend to ask for solution without trying

    – PeterM
    Nov 15 '18 at 19:49
















-4












-4








-4








How can I create a regex that will match the keyword "politics" everywhere except for in the URL below? I am trying to use a negative lookahead.



The string is a paragraph of text that looks like this:




politics are good.
http://www.foxnews.com/politics/2018/09/07/omarosa-plans-another-tape-release-next-week-report.html

politics are bad.




I want to the 1st and 3rd matches, but not the second, which is part of a URL.










share|improve this question
















How can I create a regex that will match the keyword "politics" everywhere except for in the URL below? I am trying to use a negative lookahead.



The string is a paragraph of text that looks like this:




politics are good.
http://www.foxnews.com/politics/2018/09/07/omarosa-plans-another-tape-release-next-week-report.html

politics are bad.




I want to the 1st and 3rd matches, but not the second, which is part of a URL.







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:12









benvc

6,0941827




6,0941827










asked Nov 15 '18 at 18:45









JasonJason

3982622




3982622













  • What is your regex tool?

    – anubhava
    Nov 15 '18 at 18:49











  • The stack programming seems to be raising to next level, where people tend to ask for solution without trying

    – PeterM
    Nov 15 '18 at 19:49





















  • What is your regex tool?

    – anubhava
    Nov 15 '18 at 18:49











  • The stack programming seems to be raising to next level, where people tend to ask for solution without trying

    – PeterM
    Nov 15 '18 at 19:49



















What is your regex tool?

– anubhava
Nov 15 '18 at 18:49





What is your regex tool?

– anubhava
Nov 15 '18 at 18:49













The stack programming seems to be raising to next level, where people tend to ask for solution without trying

– PeterM
Nov 15 '18 at 19:49







The stack programming seems to be raising to next level, where people tend to ask for solution without trying

– PeterM
Nov 15 '18 at 19:49














2 Answers
2






active

oldest

votes


















1














You can use a negative look behind (if your regex Tool supports it):



(?<!http:.*)politics


The regex uses a negative look behind (looking for: 'http:' followed by any char any number of times), then matches 'politics'.



This will skip matching if the line contains 'http:'.






share|improve this answer
























  • Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

    – Jason
    Nov 16 '18 at 2:32













  • What regex Tool do you use (they are very different)?

    – Poul Bak
    Nov 16 '18 at 2:37













  • regex101.com. App will be using javascript.

    – Jason
    Nov 16 '18 at 2:41













  • Nevermind. It works! I hadn't selected javascript.

    – Jason
    Nov 16 '18 at 2:42



















0














This would probably be a bit easier to do without regex at all using whatever programming language you are working with (i.e. probably easier to parse the text and ignore any strings that start with "http" and then just use string operators to match the specific text you are looking for).



That said, you could use a negative lookahead, as mentioned in your question, to exclude matches of the word when followed by a forward slash with any number of non-space characters in between. This will work if the url does not end in the word you are trying to match without a forward slash to follow. For example:



politics(?!S*/)





share|improve this answer


























  • The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

    – Jason
    Nov 15 '18 at 19:27











  • @Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

    – benvc
    Nov 15 '18 at 19:48













  • regex101.com/r/XPxPGn/2 Often times the word is in the filename.

    – Jason
    Nov 16 '18 at 2:39













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326045%2fregex-match-the-keyword-everywhere-except-inside-a-url%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









1














You can use a negative look behind (if your regex Tool supports it):



(?<!http:.*)politics


The regex uses a negative look behind (looking for: 'http:' followed by any char any number of times), then matches 'politics'.



This will skip matching if the line contains 'http:'.






share|improve this answer
























  • Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

    – Jason
    Nov 16 '18 at 2:32













  • What regex Tool do you use (they are very different)?

    – Poul Bak
    Nov 16 '18 at 2:37













  • regex101.com. App will be using javascript.

    – Jason
    Nov 16 '18 at 2:41













  • Nevermind. It works! I hadn't selected javascript.

    – Jason
    Nov 16 '18 at 2:42
















1














You can use a negative look behind (if your regex Tool supports it):



(?<!http:.*)politics


The regex uses a negative look behind (looking for: 'http:' followed by any char any number of times), then matches 'politics'.



This will skip matching if the line contains 'http:'.






share|improve this answer
























  • Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

    – Jason
    Nov 16 '18 at 2:32













  • What regex Tool do you use (they are very different)?

    – Poul Bak
    Nov 16 '18 at 2:37













  • regex101.com. App will be using javascript.

    – Jason
    Nov 16 '18 at 2:41













  • Nevermind. It works! I hadn't selected javascript.

    – Jason
    Nov 16 '18 at 2:42














1












1








1







You can use a negative look behind (if your regex Tool supports it):



(?<!http:.*)politics


The regex uses a negative look behind (looking for: 'http:' followed by any char any number of times), then matches 'politics'.



This will skip matching if the line contains 'http:'.






share|improve this answer













You can use a negative look behind (if your regex Tool supports it):



(?<!http:.*)politics


The regex uses a negative look behind (looking for: 'http:' followed by any char any number of times), then matches 'politics'.



This will skip matching if the line contains 'http:'.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:09









Poul BakPoul Bak

5,48831233




5,48831233













  • Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

    – Jason
    Nov 16 '18 at 2:32













  • What regex Tool do you use (they are very different)?

    – Poul Bak
    Nov 16 '18 at 2:37













  • regex101.com. App will be using javascript.

    – Jason
    Nov 16 '18 at 2:41













  • Nevermind. It works! I hadn't selected javascript.

    – Jason
    Nov 16 '18 at 2:42



















  • Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

    – Jason
    Nov 16 '18 at 2:32













  • What regex Tool do you use (they are very different)?

    – Poul Bak
    Nov 16 '18 at 2:37













  • regex101.com. App will be using javascript.

    – Jason
    Nov 16 '18 at 2:41













  • Nevermind. It works! I hadn't selected javascript.

    – Jason
    Nov 16 '18 at 2:42

















Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

– Jason
Nov 16 '18 at 2:32







Throws an error. * A quantifier inside a lookbehind makes it non-fixed width regex101.com/r/XPxPGn/1

– Jason
Nov 16 '18 at 2:32















What regex Tool do you use (they are very different)?

– Poul Bak
Nov 16 '18 at 2:37







What regex Tool do you use (they are very different)?

– Poul Bak
Nov 16 '18 at 2:37















regex101.com. App will be using javascript.

– Jason
Nov 16 '18 at 2:41







regex101.com. App will be using javascript.

– Jason
Nov 16 '18 at 2:41















Nevermind. It works! I hadn't selected javascript.

– Jason
Nov 16 '18 at 2:42





Nevermind. It works! I hadn't selected javascript.

– Jason
Nov 16 '18 at 2:42













0














This would probably be a bit easier to do without regex at all using whatever programming language you are working with (i.e. probably easier to parse the text and ignore any strings that start with "http" and then just use string operators to match the specific text you are looking for).



That said, you could use a negative lookahead, as mentioned in your question, to exclude matches of the word when followed by a forward slash with any number of non-space characters in between. This will work if the url does not end in the word you are trying to match without a forward slash to follow. For example:



politics(?!S*/)





share|improve this answer


























  • The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

    – Jason
    Nov 15 '18 at 19:27











  • @Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

    – benvc
    Nov 15 '18 at 19:48













  • regex101.com/r/XPxPGn/2 Often times the word is in the filename.

    – Jason
    Nov 16 '18 at 2:39


















0














This would probably be a bit easier to do without regex at all using whatever programming language you are working with (i.e. probably easier to parse the text and ignore any strings that start with "http" and then just use string operators to match the specific text you are looking for).



That said, you could use a negative lookahead, as mentioned in your question, to exclude matches of the word when followed by a forward slash with any number of non-space characters in between. This will work if the url does not end in the word you are trying to match without a forward slash to follow. For example:



politics(?!S*/)





share|improve this answer


























  • The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

    – Jason
    Nov 15 '18 at 19:27











  • @Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

    – benvc
    Nov 15 '18 at 19:48













  • regex101.com/r/XPxPGn/2 Often times the word is in the filename.

    – Jason
    Nov 16 '18 at 2:39
















0












0








0







This would probably be a bit easier to do without regex at all using whatever programming language you are working with (i.e. probably easier to parse the text and ignore any strings that start with "http" and then just use string operators to match the specific text you are looking for).



That said, you could use a negative lookahead, as mentioned in your question, to exclude matches of the word when followed by a forward slash with any number of non-space characters in between. This will work if the url does not end in the word you are trying to match without a forward slash to follow. For example:



politics(?!S*/)





share|improve this answer















This would probably be a bit easier to do without regex at all using whatever programming language you are working with (i.e. probably easier to parse the text and ignore any strings that start with "http" and then just use string operators to match the specific text you are looking for).



That said, you could use a negative lookahead, as mentioned in your question, to exclude matches of the word when followed by a forward slash with any number of non-space characters in between. This will work if the url does not end in the word you are trying to match without a forward slash to follow. For example:



politics(?!S*/)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 19:53

























answered Nov 15 '18 at 18:51









benvcbenvc

6,0941827




6,0941827













  • The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

    – Jason
    Nov 15 '18 at 19:27











  • @Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

    – benvc
    Nov 15 '18 at 19:48













  • regex101.com/r/XPxPGn/2 Often times the word is in the filename.

    – Jason
    Nov 16 '18 at 2:39





















  • The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

    – Jason
    Nov 15 '18 at 19:27











  • @Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

    – benvc
    Nov 15 '18 at 19:48













  • regex101.com/r/XPxPGn/2 Often times the word is in the filename.

    – Jason
    Nov 16 '18 at 2:39



















The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

– Jason
Nov 15 '18 at 19:27





The challenge is the word may not always be proceeded by a forward slash. It could be anywhere in the URL.

– Jason
Nov 15 '18 at 19:27













@Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

– benvc
Nov 15 '18 at 19:48







@Jason edited the answer with a version that will match the word if it is not followed by non-space characters and a forward slash (but this would match the word in a url if it was in a filename or at the end of a path that was not followed by a forward slash).

– benvc
Nov 15 '18 at 19:48















regex101.com/r/XPxPGn/2 Often times the word is in the filename.

– Jason
Nov 16 '18 at 2:39







regex101.com/r/XPxPGn/2 Often times the word is in the filename.

– Jason
Nov 16 '18 at 2:39




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326045%2fregex-match-the-keyword-everywhere-except-inside-a-url%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine