-2

ASP.Net C# newbie here. I am looking for suggestions upon constructing a condition to validate if a website URL do not contain any ISO language code or regional sub-directory e.g. /us-en, /ae-en, /gb-en, etc. using regular expression.

Sample website URLs:
• a website URL with language code - https://www.xbox.com/en-US/xbox-one?xr=shellnav
• a website URL without language code - https://www.xbox.com/xbox-one?xr=shellnav

Sample Scenario:
• website URL without any ISO language code must not be accessible and displays Page Not Found or 404 Error Message.

Much better if the suggestion is in negative approach — if the website URL do not contain any ISO language code it will execute the block of code in the statement (Page Not Found or 404 Error Message).

1 Answers1

2

Just match the URL using this regex,

\b[a-z]{2}-[A-Z]{2}\b

Demo

In case you want it case insensitive, just append (?i) in the beginning of regex,

(?i)\b[a-z]{2}-[a-z]{2}\b

Check out this C# code that prints whether URL matches or not,

List<string> strings = new List<string>();
strings.Add("https://www.xbox.com/en-US/xbox-one?xr=shellnav");
strings.Add("https://www.xbox.com/xbox-one?xr=shellnav");

var regex = new Regex(@"\b[a-z]{2}-[A-Z]{2}\b");

foreach (string s in strings)
{
    if (regex.Match(s).Success) {
        Console.WriteLine(s + " --> Matches");  // Write your code here if URL contains language code
    } else {
        Console.WriteLine(s + " --> Doesn't match"); // Else part if URL doesn't contains language code
    }
}

Prints,

https://www.xbox.com/en-US/xbox-one?xr=shellnav --> Matches
https://www.xbox.com/xbox-one?xr=shellnav --> Doesn't match
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Hi Pushpesh - Thanks for the response. Could you show me the approach? I mean the actual checking condition on the code. – hotcheesepotato Feb 24 '19 at 18:19
  • @hotcheesepotato: Hey buddy, just added a C# sample code showing the approach. Hope that helps. – Pushpesh Kumar Rajwanshi Feb 24 '19 at 18:20
  • @hotcheesepotato: Check my updated C# code as I've made it more clear and intuitive. – Pushpesh Kumar Rajwanshi Feb 24 '19 at 18:29
  • That's cool, mate. However is it possible to go with just a negative approach? I mean when the URL do not contain nor match this regex: (?i)\b[a-z]{2}-[a-z]{2}\b it will execute the block of code in the statement (regardless when the URL contains or matches the regex). – hotcheesepotato Feb 25 '19 at 07:09
  • For obtaining the negative approach, you can either use a negative look ahead regex `^(?!.*\b[a-z]{2}-[A-Z]{2}\b).*$` or code wise you can just swap the code in `if` `else` block. – Pushpesh Kumar Rajwanshi Feb 25 '19 at 08:58
  • Can I change the statement into .Failure instead of .Success for the negative approach? : if (regex.Match(s).Failure) { – hotcheesepotato Feb 26 '19 at 01:49
  • @hotcheesepotato: There is no variable named `Failure`. Instead you can negate the value using not operator `!` and write this `if (!regex.Match(s).Success)` – Pushpesh Kumar Rajwanshi Feb 26 '19 at 05:04
  • another question, what if I have an existing variable in the solution that handles or represents the URL.. What it would look like in the code? Will you still recommend to use the following line of codes? List strings = new List(); strings.Add("https://www.xbox.com/en-US/xbox-one?xr=shellnav"); strings.Add("https://www.xbox.com/xbox-one?xr=shellnav"); – hotcheesepotato Mar 05 '19 at 12:00