Regex to find URL works on Regex101.com but not in C# code










-2















I have this regex pattern to match website:



^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$



If I test it (http://www.regex101.com) with the following values:



http://www.google.com 
google.com
somesite.com


I get a match on all three values.



But this code doesn't work in C# (no matches):



var websiteRegex = new Regex(@"^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$", RegexOptions.IgnoreCase);
var stripped = stripped = phoneRegex
.Replace("http://www.google.com www.google.com somesite.com", string.Empty);









share|improve this question
























  • How are you declaring WEBSITE_PATTERN?

    – greenjaed
    Nov 14 '18 at 23:09












  • private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

    – Ian Tunbridge
    Nov 14 '18 at 23:09






  • 1





    Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

    – elgonzo
    Nov 14 '18 at 23:11







  • 1





    Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

    – Ian Tunbridge
    Nov 14 '18 at 23:18











  • The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

    – Wiktor Stribiżew
    Nov 14 '18 at 23:23















-2















I have this regex pattern to match website:



^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$



If I test it (http://www.regex101.com) with the following values:



http://www.google.com 
google.com
somesite.com


I get a match on all three values.



But this code doesn't work in C# (no matches):



var websiteRegex = new Regex(@"^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$", RegexOptions.IgnoreCase);
var stripped = stripped = phoneRegex
.Replace("http://www.google.com www.google.com somesite.com", string.Empty);









share|improve this question
























  • How are you declaring WEBSITE_PATTERN?

    – greenjaed
    Nov 14 '18 at 23:09












  • private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

    – Ian Tunbridge
    Nov 14 '18 at 23:09






  • 1





    Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

    – elgonzo
    Nov 14 '18 at 23:11







  • 1





    Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

    – Ian Tunbridge
    Nov 14 '18 at 23:18











  • The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

    – Wiktor Stribiżew
    Nov 14 '18 at 23:23













-2












-2








-2








I have this regex pattern to match website:



^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$



If I test it (http://www.regex101.com) with the following values:



http://www.google.com 
google.com
somesite.com


I get a match on all three values.



But this code doesn't work in C# (no matches):



var websiteRegex = new Regex(@"^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$", RegexOptions.IgnoreCase);
var stripped = stripped = phoneRegex
.Replace("http://www.google.com www.google.com somesite.com", string.Empty);









share|improve this question
















I have this regex pattern to match website:



^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$



If I test it (http://www.regex101.com) with the following values:



http://www.google.com 
google.com
somesite.com


I get a match on all three values.



But this code doesn't work in C# (no matches):



var websiteRegex = new Regex(@"^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$", RegexOptions.IgnoreCase);
var stripped = stripped = phoneRegex
.Replace("http://www.google.com www.google.com somesite.com", string.Empty);






c# regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:34









Poul Bak

5,48831233




5,48831233










asked Nov 14 '18 at 23:06









Ian TunbridgeIan Tunbridge

529




529












  • How are you declaring WEBSITE_PATTERN?

    – greenjaed
    Nov 14 '18 at 23:09












  • private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

    – Ian Tunbridge
    Nov 14 '18 at 23:09






  • 1





    Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

    – elgonzo
    Nov 14 '18 at 23:11







  • 1





    Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

    – Ian Tunbridge
    Nov 14 '18 at 23:18











  • The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

    – Wiktor Stribiżew
    Nov 14 '18 at 23:23

















  • How are you declaring WEBSITE_PATTERN?

    – greenjaed
    Nov 14 '18 at 23:09












  • private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

    – Ian Tunbridge
    Nov 14 '18 at 23:09






  • 1





    Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

    – elgonzo
    Nov 14 '18 at 23:11







  • 1





    Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

    – Ian Tunbridge
    Nov 14 '18 at 23:18











  • The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

    – Wiktor Stribiżew
    Nov 14 '18 at 23:23
















How are you declaring WEBSITE_PATTERN?

– greenjaed
Nov 14 '18 at 23:09






How are you declaring WEBSITE_PATTERN?

– greenjaed
Nov 14 '18 at 23:09














private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

– Ian Tunbridge
Nov 14 '18 at 23:09





private readonly string WEBSITE_PATTERN = @"^(?i)(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]1[a-z0-9]+)*.[a-z]2,5(:[0-9]1,5)?(/.*)?$";

– Ian Tunbridge
Nov 14 '18 at 23:09




1




1





Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

– elgonzo
Nov 14 '18 at 23:11






Ahem, it cannot match the string "http://www.google.com www.google.com somesite.com" because you use the ^ and $ anchors...

– elgonzo
Nov 14 '18 at 23:11





1




1





Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

– Ian Tunbridge
Nov 14 '18 at 23:18





Thanks @elgonzo I wasn't sure what those did. I was just borrowing the pattern from someone else's example. Both your comment and Poul Bak's answer were correct!

– Ian Tunbridge
Nov 14 '18 at 23:18













The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

– Wiktor Stribiżew
Nov 14 '18 at 23:23





The thing is your pattern matches the whole string that is a URL. If you tested against "http://www.google.comnwww.google.comnsomesite.com", why do you think it will work the same against "http://www.google.com www.google.com somesite.com"?

– Wiktor Stribiżew
Nov 14 '18 at 23:23












1 Answer
1






active

oldest

votes


















0














The reason is simple: regex101.com automatically apply the 'MultiLine' option, which is necessary in your case.



Your code should be:



var websiteRegex = new Regex(WEBSITE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.MultiLine);





share|improve this answer




















  • 1





    The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

    – elgonzo
    Nov 14 '18 at 23:16











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%2f53310084%2fregex-to-find-url-works-on-regex101-com-but-not-in-c-sharp-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The reason is simple: regex101.com automatically apply the 'MultiLine' option, which is necessary in your case.



Your code should be:



var websiteRegex = new Regex(WEBSITE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.MultiLine);





share|improve this answer




















  • 1





    The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

    – elgonzo
    Nov 14 '18 at 23:16
















0














The reason is simple: regex101.com automatically apply the 'MultiLine' option, which is necessary in your case.



Your code should be:



var websiteRegex = new Regex(WEBSITE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.MultiLine);





share|improve this answer




















  • 1





    The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

    – elgonzo
    Nov 14 '18 at 23:16














0












0








0







The reason is simple: regex101.com automatically apply the 'MultiLine' option, which is necessary in your case.



Your code should be:



var websiteRegex = new Regex(WEBSITE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.MultiLine);





share|improve this answer















The reason is simple: regex101.com automatically apply the 'MultiLine' option, which is necessary in your case.



Your code should be:



var websiteRegex = new Regex(WEBSITE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.MultiLine);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 23:36

























answered Nov 14 '18 at 23:11









Poul BakPoul Bak

5,48831233




5,48831233







  • 1





    The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

    – elgonzo
    Nov 14 '18 at 23:16













  • 1





    The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

    – elgonzo
    Nov 14 '18 at 23:16








1




1





The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

– elgonzo
Nov 14 '18 at 23:16






The string in the question "http://www.google.com www.google.com somesite.com" doesn't contain line-feeds, but just simple spaces as url delimiters. I just checked the source text of the question. With regard to the string in the sample code in the question RegexOptions.MultiLine will not help...

– elgonzo
Nov 14 '18 at 23:16




















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%2f53310084%2fregex-to-find-url-works-on-regex101-com-but-not-in-c-sharp-code%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo