2014-02-05 2 views
0

PHP에서 정규 표현식과 관련하여 의문의 여지가 있습니다. PHP를 사용하는 하나의 정규 표현식

$content = "START FIRST AAA SECOND AAA" 
$content_first = preg_replace('/START(.*)AAA/', 'REPLACED_STRING', $content); 
//$content_first == "REPLACED_STRING" 
$content_second = preg_replace('/START(.*?)AAA/', 'REPLACED_STRING', $content); 
//$content_second == "REPLACED_STRING SECOND AAA" 

이유는 정확히 $는 content_first $ 다를 content_second 무엇입니까? '의 목적은 무엇입니까?' 정규식에? 다음 정규식 (정말 광범위한) 및 첫 번째 중지하지 않고 문자열에있는 모든 URL을 바꿀 수 있도록 수정하려면 원하는 수 있지만 나는 단지 문자열의 첫 번째 URL을 찾습니다.) :

$url_pattern = '/# Rev:20100913_0900 github.com\/jmrware\/LinkifyURL 
    # Match http & ftp URL that is not already linkified. 
     # Alternative 1: URL delimited by (parentheses). 
     (\()      # $1 "(" start delimiter. 
     ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+) # $2: URL. 
     (\))      # $3: ")" end delimiter. 
    | # Alternative 2: URL delimited by [square brackets]. 
     (\[)      # $4: "[" start delimiter. 
     ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+) # $5: URL. 
     (\])      # $6: "]" end delimiter. 
    | # Alternative 3: URL delimited by {curly braces}. 
     (\{)      # $7: "{" start delimiter. 
     ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+) # $8: URL. 
     (\})      # $9: "}" end delimiter. 
    | # Alternative 4: URL delimited by <angle brackets>. 
     (<|&(?:lt|\#60|\#x3c);) # $10: "<" start delimiter (or HTML entity). 
     ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+) # $11: URL. 
     (>|&(?:gt|\#62|\#x3e);) # $12: ">" end delimiter (or HTML entity). 
    | # Alternative 5: URL not delimited by(), [], {} or <>. 
     (      # $13: Prefix proving URL not already linked. 
     (?:^    # Can be a beginning of line or string, or 
     | [^=\s\'"\]]   # a non-"=", non-quote, non-"]", followed by 
     ) \s*[\'"]?   # optional whitespace and optional quote; 
     | [^=\s]\s+    # or... a non-equals sign followed by whitespace. 
    )      # End $13. Non-prelinkified-proof prefix. 
     (\b      # $14: Other non-delimited URL. 
     (?:ht|f)tps?:\/\/  # Required literal http, https, ftp or ftps prefix. 
     [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]+ # All URI chars except "&" (normal*). 
     (?:     # Either on a "&" or at the end of URI. 
      (?!     # Allow a "&" char only if not start of an... 
      &(?:gt|\#0*62|\#x0*3e);     # HTML ">" entity, or 
      | &(?:amp|apos|quot|\#0*3[49]|\#x0*2[27]); # a [&\'"] entity if 
      [.!&\',:?;]?  # followed by optional punctuation then 
      (?:[^a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]|$) # a non-URI char or EOS. 
     ) &     # If neg-assertion true, match "&" (special). 
      [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]* # More non-& URI chars (normal*). 
     )*      # Unroll-the-loop (special normal*)*. 
     [a-z0-9\-_~$()*+=\/#[\]@%] # Last char can\'t be [.!&\',;:?] 
    )      # End $14. Other non-delimited URL. 
    /imx'; 

누구든지 나를 도와 주거나 올바른 방향으로 나를 이끌 수 있습니까? 정말 고맙습니다!


좋아, 나는 당신의 설명을 이해 생각 (그것을위한 타이를!), 단지 내 첫 번째 URL '는'태그 사이에 넣어 될 어떤 이유가? 코드의 나머지 :

$url_replace = '$1$4$7$10$13<a>$2$5$8$11$14</a>$3$6$9$12'; 
return preg_replace($url_pattern, $url_replace, $text); 

$text = 
http://www.youtube.com/watch?v=Cy8duEIHEig http://www.youtube.com/watch?v=Cy8duEIHEig 

경우는 첫 번째 URL은 URL로 표시됩니다. 이게 *과 관련이 있습니까??

답변

0

? 두 가지 일을합니다. .> 제 1 및 작은 일치를 발견 - 첫 번째는

ab?c 
b는 귀하의 경우

.*? 

이의 욕심 알고리즘을 비활성화에서 두 번째 옵션 또는이다

*이 예를 들어 식을 선택 할 수 있습니다 .

+0

정확히 무엇을 의미합니까? – user111671

+0

'?'뒤에 무엇이든 찾으면 바로 멈 춥니 다. –

+0

@ JoaoRaposo 당신이 대답 업데이트를 확인해 주시겠습니까? – user111671

0

content_first $ 및 $ content_second 차이가 아니라 여기에서 설명된다 What do lazy and greedy mean in the context of regular expressions?

$, 가능한 많은 문자와 일치하므로 정규식에 AAA 접미사 실제로 START 일치 후에 의미 욕심 content_first 생략. $ content_second는 PCRE가 AAA를 충족 할 때까지 모든 문자를 일치시킵니다.

+0

나는 그것을 얻었을 것이라고 생각한다. 대답 업데이트를 확인해 주시겠습니까? – user111671