2016-12-14 1 views
1

많은 답변을 보았지만 아무도 내 상황에 도움이되지 못합니다.사용 방법 | 문자열 사이를 검색하기위한 preg_match의 연산자?

Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information...

또는이 텍스트를 : https://regex101.com/r/apIT0O/1

하지만 수없는 것 :

Comments: These are the comments and they end at this full stop. You can only use the personal information...

동작하는 예제가 여기에 나는이 텍스트에서 주석을 추출해야 이것을 PHP로 가져 오십시오. 나는 함께 일할 수 있습니다 :

$pattern = "/(?<=Comments:)(.*?)(?= Remember, you can)/s"; 

그러나이 문제는 무엇입니까?

$pattern = "/(?<=Comments:)(.*?)(?= Remember, you can)|(?<=Comments:)(.*?)(?= You can)/s"; 

감사합니다.

+0

실제로 두 사람은 캡처하려는 컨텍스트를 [일치] (https://eval.in/697037)합니다. 그러나 당신의 표현에 대해 다른 접근법을 추천합니다. – hwnd

답변

0

preg_match_all을 사용하면 모든 결과를 정규식으로 얻을 수 있습니다.

이 시도 :

/(?<=Comments:)(.*?)(?= Remember, you can)|(?<=Comments:)(.*?)(?= You can)/s 

Explanation

샘플 코드 :

<?php 

$re = '/(?<=Comments:)(.*?).(?= Remember, you can)|(?<=Comments:)(.*?)(?= You can)/sm'; 
$str = 'Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information... 

Comments: These are the comments and they end at this full stop. You can only use the personal information...'; 
preg_match_all($re, $str, $matches); 
print_r($matches); 
?> 

Runt it here

+1

감사합니다 - 나는 preg_match_all을 필요로했다 – Warren

+1

실제로, 사후에 - 왜이 패턴은 preg_match_all와 함께 작동합니까 ??? – Warren

+0

@ 워렌 당신은 저의 정규식 패턴으로 제 대답을 볼 수 있습니다. – Kumar

0

당신은이 같은 preg_match를하여도 당신의 욕망 값을 얻을 수 있습니다. 여기에 코드

입니다
<?php 
$data="Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information..."; 
preg_match("/(.*)(?<=Comments:)(.*?)(?= Remember, you can)(.*)/",$data,$m); 
echo $m[2]; 
?> 

출력 :

이러한 의견은 그들이이 전체 정류장에서 끝난다.

또는 당신은

인 print_r ($ m에) 당신이 정규식 캡처 모든 값을 출력 할 수 있습니다. 호프가 도움이되기를 바랍니다.