2012-03-25 13 views
-1

正規表現で新しいもの 特定のURLに一致する必要があるURLの文字列を比較したい場合は、一致する場合はtrueを返します。例えば私のURLは、これは動作するはずGoogle MapsのURLと一致する正規表現

(コメントからの要求を含めるため)

+0

C#またはPHP?私はPHPを取る。 – Ryan

+0

C#コード、PHPコード、または正規表現パターンの後ろにありますか? –

答えて

5

が更新助けてくださいhttp(s)://map.google.{ any letter here }/maps

がstricklyその形式で上記の式と一致している必要があります:

^(http(s?)://)?maps\.google(\.|/).*/maps/.*$ 

(注)この意志リテラルワードgoogleの後に.または/のいずれかを指定できるようになり、次の両方が一致します。

ここでは0
maps.google/co.ke/maps/anything 
maps.google.co.ke/maps/anything 

これは、あなたがPHPでそれを使用する方法であるあなたが

それを理解
@" 
^   # Assert position at the beginning of the string 
(   # Match the regular expression below and capture its match into backreference number 1 
    http  # Match the characters “http” literally 
    (   # Match the regular expression below and capture its match into backreference number 2 
     s   # Match the character “s” literally 
     ?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
    ) 
    ://   # Match the characters “://” literally 
)?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
maps  # Match the characters “maps” literally 
\.   # Match the character “.” literally 
google  # Match the characters “google” literally 
(   # Match the regular expression below and capture its match into backreference number 3 
       # Match either the regular expression below (attempting the next alternative only if this one fails) 
     \.   # Match the character “.” literally 
    |   # Or match regular expression number 2 below (the entire group fails if this one fails to match) 
    /   # Match the character “/” literally 
) 
.   # Match any single character that is not a line break character 
    *   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
/maps/  # Match the characters “/maps/” literally 
.   # Match any single character that is not a line break character 
    *   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
$   # Assert position at the end of the string (or before the line break at the end of the string, if any) 
" 

を助けるためにRegexBuddyからのコメントと同じにreg元である:

そして、どのようこれがありますC#で使用します:

var subjectString = "maps.google/co.ke/maps/anything"; 
try { 
    if (Regex.IsMatch(subjectString, @"^(http(s?)://)?maps\.google(\.|/).*/maps/.*$")) { 
     // Successful match 
    } else { 
     // Match attempt failed 
    } 
} catch (ArgumentException ex) { 
    // Syntax error in the regular expression 
} 

また、あなたのgooグレーはmap.googleです - それはmaps.googleではありませんか?私はあなたがあなたのコメントで使用した入力に基づいて私の答えでそう仮定しました。

+0

私はそれを試しました、その実装は次のとおりでした – user1058997

+0

私はそれを試して、私はこのように実装しました<?php $ string = "http://maps.google/co.ke/maps"; if(preg_match( '%^ https ?: // maps \ .google \ .. */maps $'、$ string)){ echo "成功した一致"; } else { echo "試行に失敗しました"; } ?>それが失敗したマッチの試みをエコー保ちます。警告: "警告:preg_match()[function.preg-match]:C:\ wamp \ www \ c \ s.phpの行末に終了デリミタ '%'はありません。もう一度この方法で一致させたい私は忘れたと思うhttp://maps.google/{anything}/maps/{anythinh} – user1058997

+0

@ user1058997それは2つの理由のために働いていない。 1)あなたのパターンの最後に '%'がありません。(警告のように)2)あなたが求めようとしている '$ string'があなたと違っています。あなたは '' google 'の後に '.'があるように頼んだが、' 'google'の後に'/'とマッチさせようとしている。どちらがいいですか?また、私は最後に何かを許すように答えを更新し(あなたが尋ねたように)、 'https://'オプションを(オプションで 's'を使って)作るようにします。 – Robbie

関連する問題