【解決方法】URL を検証するための REGEX


次の形式の URL を検証する正規表現が必要です

Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
https://www.site.com
Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
http://domain.site.com
https://domain.site.com
http://www.domain.site.com
https://www.domain.site.com
site.com domain.site.com
Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
https://www.site.com/path/to/dir/
Login | HSTS Redirection Community
HSTS Redirection Community Customer Secure Login Page. Login to your HSTS Redirection Community Customer Account.
Login | HSTS Redirection Community
HSTS Redirection Community Customer Secure Login Page. Login to your HSTS Redirection Community Customer Account.
http://domain.site.com/path/to/dir/
https://domain.site.com/path/to/dir/
http://www.domain.site.com/path/to/dir/
https://www.domain.domain.site.com/path/to/dir/
site.com/path/to/dir/ domain.site.com/path/to/dir/
Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
Salesforce Einstein 1 Platform for Application Development
The Einstein 1 Platform unifies Data, AI, CRM, Development, and Security into a single, comprehensive platform. Learn more about Salesforce’s application develo...
Login | HSTS Redirection Community
HSTS Redirection Community Customer Secure Login Page. Login to your HSTS Redirection Community Customer Account.
Login | HSTS Redirection Community
HSTS Redirection Community Customer Secure Login Page. Login to your HSTS Redirection Community Customer Account.
http://domain.site.com/path/to/file.html
https://domain.site.com/path/to/file.html
http://www.domain.site.com/path/to/file.html
https://www.domain.domain.site.com/path/to/file.html
site.com/path/to/file.html domain.site.com/path/to/file.html

そして相対パス

./path/to/file.html
./path/to/dir/
./path/to/dir
path/to/file.html
path/to/dir/
path/to/dir

(ftp:// 不可)

ファイルの拡張子は、html、php、gif、jpg、png です。

私の正規表現の知識では、これを達成するのに1年かかります(それ以上ではないにしても)。 昨日、相対 URL の正規表現を実行するのに 1 時間以上かかりましたが、思い通りにはなりませんでした。 REGEX を知らなかったことを謝る必要があるような気がします。 🙁

URL が実際にどこを指していなくても問題はないことに注意してください。私の主な関心事は形式です。 例が示すように、フォーマットがそれら(およびそれらのみ)にある必要があります(これは完全なリストです)。 それらだけが私が使用し、必要とするものですが、別の形式を検証する場合は問題ありません(http / https … ftp、ftps、またはその他のもののみが許可されていない限り)。

解決策 1

これを試して:

(^(http[s]?://)?([w]{3}[.])?([a-z0-9]+[.])+com(((/[a-z0-9]+)*(/[a-z0-9]+/))*([a-z0-9]+[.](html|php|gif|png))?)$)|(^([.]/)?((([a-z0-9]+)/?)+|(([a-z0-9]+)/)+([a-z0-9]+[.](html|php|gif|png)))?$)

解決策 2

これを試すことができます(解決策#1より少し短い):

PHP
^((https?:[/][/])?\w+[.])+com|((https?:[/][/])?\w+[.])+com[/]|[.][/])?\w+([/]\w+)*([/]|[.]html|[.]php|[.]gif|[.]jpg|[.]png)?)$


[EDIT1]

正しいパターンは

TXT
^((https?:[/][/])?(\w+[.])+com|((https?:[/][/])?(\w+[.])+com[/]|[.][/])?\w+([/]\w+)*([/]|[.]html|[.]php|[.]gif|[.]jpg|[.]png)?)$

括弧に誤りがありました。
[/EDIT1]

これは次のように分解されます ( はそれぞれのパターンに置き換える必要があります)。

TXT
<valid>        = <prefix>|(<prefix>[/]|[.][/])?<path>
<prefix>       = (https?:[/][/])?<host>
<host>         = \w+([.]\w+)*[.]com
<path>         = \w+([/]\w+)*([/]|[.]html|[.]php|[.]gif|[.]jpg|[.]png)?

[EDIT2]

クエリはパスの後、またはパスが存在しない場合はプレフィックスの後に続きます。プレフィックスのない parh に対してクエリを実行することはできません。
[/EDIT2]

[EDIT3]

複雑さを管理するには、パターンを個別の変数に分割し、完全なパターンに連結します。 これにより、パターン全体の一部をテストできます。

例えば

PHP
// query
$rx_qpart = '\\w+=[^&]*';
$rx_qhead = '[?]'.$rx_qpart;
$rx_qnext = '[&]'.$rx_qpart;
$rx_qtail = '('.$rx_qnext.')*';
$rx_query = '('.$rx_qhead.$rx_qtail.')?'; // *** to be used in the main pattern
// path
$rx_ppart = '\\w+';
$rx_phead = $rx_ppart;
$rx_pnext = '[/]'.$rx_ppart;
$rx_ptail = '('.$rx_pnext.')*';
$rx_pdend = '[/]';
$rx_pfend = '[.]html|[.]php|[.]gif|[.]jpg|[.]png';
$rx_pend  = '('.$rx_pdend.'|'.$rx_pfend.')?':
$rx_rpath = $rx_phead.$rx_ptail.$rx_pend;                     // *** to be used in the main pattern
$rx_qpath = $rx_phead.$rx_ptail.'('.$rx_pfend.')?'.$rx_query; // *** to be used in the main pattern
// host
$rx_hpart = '\\w+';
$rx_hhead = $rx_hpart;
$rx_hnext = '[.]'.$rx_hpart;
$rx_htail = '('.$rx_hnext.')*';
$rx_top   = '[.]com'; // I suggest to replace by $rx_top = $rx_hnext;
$rx_host  = $rx_hhead.$rx_htail.$rx_top; // *** to be used in the main pattern
// protocol
$rx_protocol = '(https?:[/][/])?'; // *** to be used in the main pattern
// prefix
$rx_prefix = $rx_protocol.$rx_host;
// **** full pattern ****
$rx_url = '^('.$rx_prefix.'[/]?';
          .'|'.$rx_prefix.'[/]'.$rx_qpath
          .'|'.$rx_prefix.$rx_query
          .'|'.$rx_rpath
          .'|'.'[.][/]'.$rx_rpath
          .')$';

ノート: 次のような囲まれた特殊文字が PHP インタープリターによってさらに解釈されないようにするには、一重引用符を使用する必要があります。 &など
[/EDIT3]

乾杯
そして私

解決策 3

これを簡単に試すことができます:

C#
Uri.IsWellFormedUriString(YourURLString, UriKind.RelativeOrAbsolute)

見る MSDN

解決策 12

何度も回答を投稿しようとしましたが、プレビューで、投稿されたテキストが変更されていると思われ続けました??
それで、もう一度試すためにそれらを削除しましたが、ブロックされたので、これはほとんど私が投稿しようとしていたものです…

^((https?:\/\/)?([a-z0-9]+\.?)*[a-z0-9]+\.(com|php)(\/([a-z0-9]+\/)+)?|(\.\/)?([a-z0-9]+\/)+[a-z0-9]*$|(\.\/)?([a-z0-9]+\/)+)([a-z0-9]+\.(html?|php|png|jpg|gif))?$

正しく表示/動作しない場合は、代わりに… https://regex101.com/r/jOvcz0/1にアクセスしてください
そのため、編集されていない表現を表示できます。また、下部にメモがあり、一致しないものを推測できます。
誰かが正規表現で使用するのに最適な事前タグについてアドバイスを持っている場合、それは とても とても有難い。

.html や「クエリ文字列」についてはよくわかりませんが、サンプルを投稿すれば、専門家が修正を手伝ってくれると確信しています。
真実のプレビューを得ることが、いくつかの質問に答えようとするよりも 1000 倍も困難だったことは非常に残念です。
「解決策を提出する」をクリックした後、最終的に最後のプレビューを見ないことを学びました。

コメント

タイトルとURLをコピーしました