locked
Need a regular expression that validates file paths, FQDN, IP addresses all along RRS feed

  • Question

  • User254389092 posted

    Hi,

    I have a typical issue. I need a C# regular expression that validates file paths, FQDN, IP addresses. I have tried the following:

    ^\\(\\[\w-\.]){1,}(\\[\w-()](\s[\w-()]))(\\(([\w-()](\s[\w-()]))\.[\w])?)?$ - Initial test

    ^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$) - works for FQDNs of any kind but fails file paths.

    ^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$ - Current trial but fails.

    I tried online where I can find individual regular expressions but not something that works for all three.  The examples I need to satisfy are HTTP://<my test path>

    c:\Dev\temp.doc

    In my code, the implementation will be as:

    [LocalizedRegularExpression(@"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)", "Path_issue")]

    I need to replace the path above.

    As an update: 

    (?=^.{4,253}$)(^((?!-)[a-zA-Z0-9]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z0-9]{2,63}$) - This helps for URL without http:// or https:// and IP addresses. 
    I am looking for http:// or https:// and file path types in this as well.

    I appreciate any help.

    Luc

     

    Tuesday, September 17, 2019 9:27 AM

Answers

  • User-719153870 posted

    Hi Luc,

    You can learn Regular Expression Language and JavaScript Regular Expressions, this will help you build your own regular expressions.

    As for the problem you are trying to solve, there are two ways in JS for you.

    One is that you can use if() else if() else() in JS to do mulitple times of validation;

    Another is to use only one regular expression which i won't recommend in some special cases, this might cause some validation issues.

    I built a demo to show how you can apply these two ways to validate file paths, FQDN ,IP address and Urls:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function check(e) {
                var l = document.getElementById("Label1");
                var v = e.value;
                var expFilePath = /(\\\\?([^\\/]*[\\/])*)([^\\/]+)$/g;
                var expFQDN = /^(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)$/;
                var expIPAddress = /^(?=.*[^\.]$)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.?){4}$/igm;
                var expURL = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm;
                if ((expFQDN.test(v))) {
                    l.innerText = "This is a FQDN";
                }
                else if ((expFilePath.test(v))) {
                    l.innerText = "This is a File Path";
                }
                else if ((expIPAddress.test(v))) {
                    l.innerText = "This is an IP Address";
                }
                else if ((expURL.test(v))) {
                    l.innerText = "This is a URL";
                }
                else {
                    l.innerText = "False";
                }
            }
    
            function check1(e) {
                var l = document.getElementById("Label2");
                var v = e.value;
                var exp = /^(\\\\?([^\\/]*[\\/])*)([^\\/]+)|(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)|(?=.*[^\.]$)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.?){4}|(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/igm;
                if ((exp.test(v))) {
                    l.innerText = "True";
                }
                else { l.innerText = "False"; }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                Multi expressions<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled" Width="200px" onblur="check(this)"></asp:TextBox>
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /><br />
                One expression<asp:TextBox ID="TextBox2" runat="server" AutoCompleteType="Disabled" Width="200px" onblur="check1(this)"></asp:TextBox>
                <asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
                <br />
                File Path Sample: c:\Dev\temp.doc<br />
                FQDN Sample: en.wikipedia.org<br />
                IP Addresses Sample: 192.168.1.0<br />
                URL Sample: http://www.example.com<br />
            </div>
        </form>
    </body>
    </html>

    Below is the result of this demo:

    Hope this would help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2019 2:51 AM