2016-10-12 14 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

あなたが明らかに_can_していることは、_two_個の正規表現を適用することです:文字列の先頭から任意の部分文字列を取得するためのもの、取得された部分文字列をさらに下に一致させようとするものです。 – 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を使用することです。

関連する問題