2012-03-25 3 views
-1

정규식이 새롭다 일치하는 경우 특정 URL과 일치해야하는 URL 문자열을 비교하고, 그렇지 않으면 true를 반환합니다. 예를 들어, 내 URL을 http(s)://map.google.{ any letter here }/mapsGoogle지도 URL과 일치하는 정규 표현식

strickly하는 형식으로 위의 표현과 일치해야

도와주세요입니다
+0

C# 또는 PHP? 나는 그것을 PHP로 가져 간다. – Ryan

+0

C# 코드, PHP 코드 또는 정규식 패턴 이후입니까? –

답변

5

업데이트]이 작동합니다

(의견의 요청을 포함하는) :

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

참고이 의지를 이제는 . 또는 /이라는 글자 단어 뒤에 google을 허용하므로 다음 두 항목이 일치합니다.

여기
maps.google/co.ke/maps/anything 
maps.google.co.ke/maps/anything 

0 당신이

@" 
^   # 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) 
" 

이것은 당신이 PHP에서 그것을 사용하는 것이 어떻게 이해하는 데 도움이 RegexBuddy 의견과 같은 등록의 예입니다 :

$subject = 'maps.google/co.ke/maps/anything'; 
if (preg_match('%^(http(s?)://)?maps\.google(\.|/).*/maps/.*$%', $subject)) { 
    echo 'Successful match'; 
} else { 
    echo 'Match attempt failed'; 
} 

을 그리고이 그 방법이다 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에 대한 URL gle은 map.google입니다. maps.google일까요? 나는 당신이 아래의 당신의 코멘트에서 사용한 입력에 근거하여 나의 대답에서 그렇게 가정했다.

+0

나는 그것을 시도했다, 그것의 구현은 다음과 같다. – user1058997

+0

나는 이것을 시도했다. 나는 이렇게 구현했다. 하지만 실패 일치 시도를 해본 유지한다. 경고 : "경고 : 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

관련 문제