Jump to content
wuwuxin

Does Delphi have a counterpart for C#/.NET Uri.IsWellFormedUriString?

Recommended Posts

In C#, there is a 

Uri.IsWellFormedUriString

Does Delphi provide a similar utility?

Share this post


Link to post
3 hours ago, Dave Novo said:

You can use the built in Regex tools in delphi and use the Regex from the following link

https://stackoverflow.com/questions/30847/regex-to-validate-uris

@Dave Novo

Dave - many thanks for the heads up.  Following your advice, I resorted to RegEx, using the following code, and it works well for me. I posted the code here, in case someone may have the same needs. Thanks again.

procedure MyFunkyRegExProcedure;

const
  sUrlRegEx: String =
    '^'#10 +
        '(# Scheme'#10 +
        ' [a-z][a-z0-9+\-.]*:'#10 +
        ' (# Authority & path'#10 +
        '  //'#10 +
        '  ([a-z0-9\-._~%!$&''()*+,;=]+@)?              # User'#10 +
        '  ([a-z0-9\-._~%]+                             # Named host'#10 +
        '  |\[[a-f0-9:.]+\]                             # IPv6 host'#10 +
        '  |\[v[a-f0-9][a-z0-9\-._~%!$&''()*+,;=:]+\])  # IPvFuture host'#10 +
        '  (:[0-9]+)?                                   # Port'#10 +
        '  (/[a-z0-9\-._~%!$&''()*+,;=:@]+)*/?          # Path'#10 +
        ' |# Path without authority'#10 +
        '  (/?[a-z0-9\-._~%!$&''()*+,;=:@]+(/[a-z0-9\-._~%!$&''()*+,;=:@]+)*/?)?'#10 +
        ' )'#10 +
        '|# Relative URL (no scheme or authority)'#10 +
        ' ([a-z0-9\-._~%!$&''()*+,;=@]+(/[a-z0-9\-._~%!$&''()*+,;=:@]+)*/?  # Relative path'#10 +
        ' |(/[a-z0-9\-._~%!$&''()*+,;=:@]+)+/?)                             # Absolute path'#10 +
        ')'#10 +
        '# Query'#10 +
        '(\?[a-z0-9\-._~%!$&''()*+,;=:@/?]*)?'#10 +
        '# Fragment'#10 +
        '(\#[a-z0-9\-._~%!$&''()*+,;=:@/?]*)?'#10 +
        '$';
begin
  var LRegEx := TRegEx.Create(sUrlRegEx, [roIgnoreCase, roIgnorePatternSpace]);
  var LMatch := LRegEx.Match(FUrl);
end;

 

  • Thanks 2

Share this post


Link to post
On 4/19/2021 at 12:57 AM, wuwuxin said:

var LRegEx := TRegEx.Create(sUrlRegEx, [roIgnoreCase, roIgnorePatternSpace]);

var LMatch := LRegEx.Match(FUrl);

Or the one-liner:

TRegEx.IsMatch(FUrl, sUrlRegEx, [roIgnoreCase, roIgnorePatternSpace])

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×