2011-11-23 5 views
3

이것은 작동하지 않는 코드입니다. 이제 아무 것도 표시하지 않아야합니다. 그러나 여전히 링크가 표시됩니다.정규 표현식 (는 preg_match)는

+1

ur 질문에 따르면 reg exp와 일치하면 아무 것도 표시하지 말고 반대 방향으로 만 수행하면됩니다. 시작과 끝 부분에 공백이 있으면 출력하십시오. :) – mithunsatheesh

답변

2

3은 공백이 아니므로 /는 http : /가 공백이 아니기 때문에 ID12와도 일치 할 수 있습니다. 시도해 볼 수 있습니다 :

preg_match_all('/^\S*\/videosite\.com\/(\w+)\S*$/i', $matchWith, $matches); 
+0

대단히 고맙습니다.이 답변은 제가 찾고 있던 답변이었습니다. – Helena

2

공백이 있으면 표시하지 않으려합니다. 이 같은 것은 작동해야하며 테스트하지 않았습니다.

preg_match_all('/^\S+?videosite\.com\/(\w+)\S+?$/i', $matchWith, $matches); 
+0

굉장합니다. 내가 찾는 대답. 그러나 또 다른 문제가 있습니다. ID가 ID123이면 (링크에서) ID12 만 표시됩니다. 감사합니다 – Helena

+0

해야합니다 : /\s*\/videosite\com\/(\w+)\s*/i. – Godwin

1

시도해 볼 수 있습니다. 그것은 작동 :

if (preg_match('%^\S*?/videosite\.com/(\w+)(?!\S+)$%i', $subject, $regs)) { 
    #$result = $regs[0]; 
} 

하지만 난이 게시 한 후, 당신은 당신의 질문을 :) 업데이트 할 점은 긍정적이다

설명 :

" 
^   # Assert position at the beginning of the string 
\S   # Match a single character that is a “non-whitespace character” 
    *?   # Between zero and unlimited times, as few times as possible, expanding as needed (lazy) 
\/   # Match the character “/” literally 
videosite # Match the characters “videosite” literally 
\.   # Match the character “.” literally 
com   # Match the characters “com” literally 
\/   # Match the character “/” literally 
(   # Match the regular expression below and capture its match into backreference number 1 
    \w   # Match a single character that is a “word character” (letters, digits, etc.) 
     +   # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
) 
(?!   # Assert that it is impossible to match the regex below starting at this position (negative lookahead) 
    \S   # Match a single character that is a “non-whitespace character” 
     +   # Between one 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) 
" 
+0

정말 대단한 답변입니다! 믿을 수 없는! 그러나 나는 정말 바보 같은 놈이고 내 코드에이 코드를 통합 할 수 없다. 하지만 다른 코드가 작동했지만 여전히 감사합니다. 여러분들은 대단합니다. – Helena

0

아마 이것을 사용하는 것이 더 간단 할 것 정규식 :

'/^http:\/\/videosite\.com\/(\w+)$/i' 

나는 전자 공백은 http 앞에 있고, 공백은 디렉토리 다음에옵니다. 따라서 ^ 문자를 사용하여 문자열이 http로 시작해야 함을 나타내야하고 마지막에 $ 문자를 사용하여 문자열이 단어 문자로 끝나야 함을 나타낼 수 있습니다.

+0

내가 찾고 있던 대답이 있습니다. 아직도 대단히 감사합니다 :) – Helena