2016-10-12 1 views
1

제공 한 동일한 세트를 찾은 다음 문자 세트 정확한 을 기억하고 나중에 찾기 위해 줄에.정규식은 문자의 집합을 적용하고 기억하고 내가 정규식 <em>문자</em>의 수를 수용 할

예를 들어 Regex에서 'TheseCharacters'로 시작하는 줄을 본 경우 나중에 'TheseCharacters'가 나타나는 줄을 찾으면 줄과 일치 시키길 원합니다.

예 (이러한 모든 일치합니다) :

TheseCharacters, I really enjoy TheseCharacters.

Dog1, My favorite word is Dog1.

다음 것 하지 경기 :

Cakeman, oh I enjoy cakeboy.

는 정규식의 범위를 벗어난이인가 , 또는 거기에있다 어떻게 동적으로 이렇게합니까?

+1

이론 과학은 이것이 정규식을 사용하여 가능하지 않음을 분명히 말해줍니다. 이를 위해서는 일종의 메모리가 필요합니다. 그래서 유한 상태 머신 대신 적어도 튜링 머신이 필요합니다. 당신이 기술하는 문제는 비정규적인 문제이며, 정규 언어로 해결하기에는 너무 복잡합니다. 그것은 입증 될 수 있습니다, 그 주위에 방법이 없습니다. – arkascha

+0

그룹화 및 역 참조로 무언가를 할 수 있습니다. –

+0

당신이 분명히 할 수있는 것은 별도의 정규 표현식을 적용하는 것입니다 : 하나는 문자열의 시작 부분에서 임의의 부분 문자열을 캡처하고, 나머지는 캡처 된 부분 문자열을 더 아래로 일치 시키려고 시도합니다. – arkascha

답변

0

당신이하고 싶은 것을 말하기는 조금 어렵지만, 내가 이해 한 것으로부터 그룹화와 역 참조를 사용하여이를 수행 할 수 있습니다. 이런 식으로 뭔가 :

String 1 matches 1 times: Array 
(
    [0] => TheseCharacters, I really enjoy TheseCharacters 
    [1] => TheseCharacters 
) 

String 1 matches 1 times: Array 
(
    [0] => TheseCharacters, I really enjoy TheseCharacters 
    [1] => TheseCharacters 
) 

String 2 matches 1 times: Array 
(
    [0] => TheseCharacters, I really enjoy TheseCharacters and some others 
    [1] => TheseCharacters 
) 

String 3 matches 0 times: Array 
(
) 

String 4 matches 0 times: Array 
(
) 

데모 : https://3v4l.org/upNhm

그리고 여기 패턴의 설명 : https://regex101.com/r/DuTbyn/2

그리고 그것은 정말 "변수"아니다이 출력을 생성

<?php 
$pattern = '/^(\b\w+\b).*\b\1\b.*/i'; 

//should match 
$string = "TheseCharacters, I really enjoy TheseCharacters"; 
$result = preg_match($pattern, $string, $matches); 
echo "String 1 matches {$result} times: ".print_r($matches,true)."\n"; 

//match only with case insensitive flag, not an exact match in case 
$string = "TheseCharacters, I really enjoy thesecharacters"; 
$result = preg_match($pattern, $string, $matches); 
echo "String 1 matches {$result} times: ".print_r($matches,true)."\n"; 

//should match, doesn't require TheseCharacters to be at the end of the string. 
$string = "TheseCharacters, I really enjoy TheseCharacters and some others"; 
$result = preg_match($pattern, $string, $matches); 
echo "String 2 matches {$result} times: ".print_r($matches,true)."\n"; 

//no match, TheseCharacters has been changed to TheseLetters 
$string = "TheseCharacters, I really enjoy TheseLetters"; 
$result = preg_match($pattern, $string, $matches); 
echo "String 3 matches {$result} times: ".print_r($matches,true)."\n"; 

//no match, additional letters has been added to TheseCharacters 
$string = "TheseCharacters, I really enjoy TheseCharactersasdf"; 
$result = preg_match($pattern, $string, $matches); 
echo "String 4 matches {$result} times: ".print_r($matches,true)."\n"; 

저장되고있다. 그것은 나중에 그룹 번호로 참조 할 수있는 그룹입니다. 처음에는 문자/숫자의 첫 번째 그룹을 문자열의 첫 번째 시작 (^(\b\w+\b))과 비교합니다. 그 다음에는 임의의 수의 문자가 따라오고 나중에 첫 번째 그룹에 포착 된 내용과 일치시킵니다. 일치하는 전체 문자열은 $matches[0]에서 사용할 수 있으며 반복 문자열은 $matches[1]에서 사용할 수 있습니다.

당신이하려고하는 것에 대해 더 많이 알지 못해도, 이것은 거의 유일한 방법입니다. 다른 방법으로는 각 단어를 개별 단어로 배열로 분할하거나 각 단어를 카운트하려면 array_count_values을 사용하면됩니다.