locked
Need a Regular expression which passes FQDN, filepath, URL as well. RRS feed

  • Question

  • User254389092 posted

    Hi Team,

    I have started working on a Regular expression and I am unable to create one. I need a regular expression that passes all the samples.

    \MT-SLO-X004\Share
    \MT-SLO-X004.slo.company.corp\Share
    C:\temp\share

    and also http(s)://www.sample.com

    Right now, I am using 

    ^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(http:\/\/.+)|(\w:\\.+)$

    My Line of code is: 

    [LocalizedRegularExpression(@"^\\(\\[\w-]+){1,}(\\[\w-()]+(\s[\w-()]+)*)+(\\(([\w-()]+(\s[\w-()]+)*)+\.[\w]+)?)?$", "VM_REPORTING_REPORTSUBSCRIBESERVERVIEWMODEL_PATH_ERROR")]

    I am attempting multiple entries to check.

    But it mainly solves the URL issue. I have also tried:

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

    <div>^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$</div> <div></div> <div>can anyone please help me with a Regular expression that validates a URL, a file path (remote machine as well), an FQDN as well. </div> <div></div> <div>Luc</div>

    Wednesday, October 9, 2019 10:48 AM

Answers

  • User288213138 posted

    Hi Hi Lucifer_deep,

    Unfortunately, This is still not working. I need IP address and FQDN specific to get my work done.

    I made demo for you:

    <script>
            function check(e) {
                var lable = document.getElementById("Label1");
                var v = e.value;
                var exp = /^(\\\\?([^\\/]*[\\/])*)([^\\/]+)|(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*|(?=.*[^\.]$)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.?){4}|(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/igm;
    
                if ((exp.test(v)))
                {
                    lable.innerText = "True";
                }
                else
                {
                    lable.innerText = "False";
                }
            }
        </script>
    
    <asp:TextBox ID="TextBox1" runat="server" Width="300px" onblur="check(this)"></asp:TextBox><br />
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <br />
                File Path Sample: C:\temp\share<br />
                FQDN Sample: \MT-SLO-X004\Share<br />
               \MT-SLO-X004.slo.company.corp\Share<br />
                IP Addresses Sample: 122.22.22.22<br />
                URL Sample: http://www.sample.com<br />

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 15, 2019 7:32 AM

All replies

  • User288213138 posted

    Hi Lucifer_deep,

    Lucifer_deep

    can anyone please help me with a Regular expression that validates a URL, a file path (remote machine as well), an FQDN as well. 

    You can write separate regular expressions and associate them with "|" .

    Validate a URL:[a-zA-z]+://[^\s]*
    
    Validate a file path:^[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*$
    
    Validate FQDN: [a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?

    Regular expression: ^[a-zA-z]+://[^\s]*|[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*|[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?$

    Best regards,

    Sam

    Thursday, October 10, 2019 7:21 AM
  • User254389092 posted

    Thanks samwu,

    unfortunately, this expression handles the file path but not the other examples. My test cases are:

    FQDN: \MT-SLO-X004\Share
    FQDN: \MT-SLO-X004.slo.company.corp\Share
    file path: C:\temp\share

    URL: https://website.com

    only file path worked out with the provided expression. 

    individually, somethings work but when I merge with |, things are falling apart. Any help is appreciated. 

    I have tried: 

    /^(\\\\?([^\\/]*[\\/])*)([^\\/]+)|(?=^.{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\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
    Thursday, October 10, 2019 7:45 AM
  • User288213138 posted

    Hi lucifer_deep,

    Lucifer_deep

    unfortunately, this expression handles the file path but not the other examples.

    Lucifer_deep

    FQDN: \MT-SLO-X004\Share
    FQDN: \MT-SLO-X004.slo.company.corp\Share

    As far as i know, FQDN is composed of host name + domain name. This is not FQDN. 

    You can refer to this link:https://en.wikipedia.org/wiki/Fully_qualified_domain_name

    If you want to validate a FQDN, you can try this expression:

    Validate FQDN: ^(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)$

    Regular expression: [a-zA-z]+://[^\s]*|[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*|(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)

    The result:

    My test tool:https://www.freeformatter.com/regex-tester.html

    Best regards,

    Sam

    Thursday, October 10, 2019 8:45 AM
  • User254389092 posted

    Hi,

    I would prefer a regex which might work on my examples as well.

    Thursday, October 10, 2019 11:04 AM
  • User288213138 posted

    Hi Lucifer_deep,

    You can try below regular expression:

    Validate FQDN: (((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*


    Regular expression: ([a-zA-z]+://[^\s]*)|([a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*)|((((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s)

    The result:

    Best regards,

    Sam

    Friday, October 11, 2019 8:44 AM
  • User254389092 posted

    That was close Samwu, 

    It works on the website you used to migrate but not on the VS. My FQDNs:

    \\MT-SLO-X004\Share
    \\MT-SLO-X004.slo.mitratech.corp\Share

    still, are invalid on Visual Studio. 

    My code in VS where I implemented the Regular expression is:

    /// <summary>
          /// The path to use
          /// </summary>
          [LocalizedDisplay("VM_xxxxVIEWMODEL_PATH")]
          [LocalizedRequired("VM_REPORTING_VIEWMODEL_PATH_REQUIRED")]
          [LocalizedRegularExpression(@"^\\(\\[\w-]+){1,}(\\[\w-()]+(\s[\w-()]+)*)+(\\(([\w-()]+(\s[\w-()]+)*)+\.[\w]+)?)?$", "VM_REPORTING_REPORTxxxxVIEWMODEL_PATH_ERROR")]

    I hope you can direct me in the right direction where I am wrong. Please help me with this</div>

    Monday, October 14, 2019 11:14 AM
  • User254389092 posted

    Hi,

    Unfortunately, This is still not working. I need IP address and FQDN specific to get my work done.

    Mainly: 

    \MT-SLO-X004\Share
    \MT-SLO-X004.slo.company.corp\Share

    122.22.22.22

    those are the samples I need to work with on my code. The last regular expression provided works on online checks but not on my code. Please help me. 

    Luc 

    Tuesday, October 15, 2019 6:23 AM
  • User288213138 posted

    Hi Hi Lucifer_deep,

    Unfortunately, This is still not working. I need IP address and FQDN specific to get my work done.

    I made demo for you:

    <script>
            function check(e) {
                var lable = document.getElementById("Label1");
                var v = e.value;
                var exp = /^(\\\\?([^\\/]*[\\/])*)([^\\/]+)|(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*|(?=.*[^\.]$)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.?){4}|(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/igm;
    
                if ((exp.test(v)))
                {
                    lable.innerText = "True";
                }
                else
                {
                    lable.innerText = "False";
                }
            }
        </script>
    
    <asp:TextBox ID="TextBox1" runat="server" Width="300px" onblur="check(this)"></asp:TextBox><br />
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <br />
                File Path Sample: C:\temp\share<br />
                FQDN Sample: \MT-SLO-X004\Share<br />
               \MT-SLO-X004.slo.company.corp\Share<br />
                IP Addresses Sample: 122.22.22.22<br />
                URL Sample: http://www.sample.com<br />

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 15, 2019 7:32 AM
  • User254389092 posted

    Hi samwu,

    My code with your new expression looks like this:

    /// <summary>
    /// The path to use
    /// </summary>
    [LocalizedDisplay("VM_REPORTING_REPORTSUBSCRIBESERVERVIEWMODEL_PATH")]
    [LocalizedRequired("VM_REPORTING_REPORTSUBSCRIBESERVERVIEWMODEL_PATH_REQUIRED")]
    [LocalizedRegularExpression(@"(\\\\?([^\\/]*[\\/])*)([^\\/]+)|(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*|(?=.*[^\.]$)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.?){4}|(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+", "VM_REPORTING_REPORTSUBSCRIBESERVERVIEWMODEL_PATH_ERROR")]
    public string Path { get; set; }

    This is not the right way I guess, Because, I still have the error. Can you please correct my Line if anything is wrong in there?

    Luc

    Tuesday, October 15, 2019 7:46 AM
  • User288213138 posted

    Hi Lucifer_deep,

    LocalizedRegularExpression

    Can you tell me what is LocalizedRegularExpression?

    Best regards,

    Sam

    Tuesday, October 15, 2019 8:21 AM
  • User254389092 posted

    It should be a normal Regular expression. 

    public LocalizedRegularExpressionAttribute(string pattern, string key)
    : base(pattern)
    {
    this.LocalizedKey = key;
    }

    That is the definition from my LocalizedRegularExpressionattribute.cs file. I have seen it. Hope this helps.

    Luc

    Tuesday, October 15, 2019 8:37 AM
  • User288213138 posted

    Hi Lucifer_deep,

    It should be a normal Regular expression. 

    public LocalizedRegularExpressionAttribute(string pattern, string key)
    : base(pattern)
    {
    this.LocalizedKey = key;
    }

    That is the definition from my LocalizedRegularExpressionattribute.cs file. I have seen it. Hope this helps.

    Luc

    I mean how do you validate your regular expression? can you show me your code?

    Best regards,

    Sam

    Tuesday, October 15, 2019 9:20 AM
  • User254389092 posted

    The validation will be automatic, so the code will be part of the framework. All I need to do is to find a regex that will match these:

    \\MT-SLO-X004\Share
    \\MT-SLO-X004.slo.company.corp\Share
    C:\temp\share

    And then test it with the same values. This is what the Subject Matter Expert said.

    Luc

    Tuesday, October 15, 2019 9:46 AM
  • User288213138 posted

    Hi lucifer_deep,

    The validation will be automatic, so the code will be part of the framework.

    I mean how do you automatically verify it? What method is used?

    \\MT-SLO-X004\Share
    \\MT-SLO-X004.slo.company.corp\Share

    Is this string preceded by a "/" or two "/"? You only have a "/" above.

    Best regards,

    Sam

    Wednesday, October 16, 2019 1:39 AM