Php Regex接受任何字符集,然后记住并查找所提供的相同字符集

Php Regex接受任何字符集,然后记住并查找所提供的相同字符集,php,regex,preg-match,Php,Regex,Preg Match,我希望正则表达式接受任意数量的字符,然后记住这组字符,然后在后面的行中查找它 例如,如果正则表达式看到行以“TheseCharacters”开头,那么如果它看到“TheseCharacters”出现在行的后面,我希望它与行匹配 示例(所有这些都将匹配): 这些角色,我真的很喜欢这些角色。 Dog1,我最喜欢的单词是Dog1。 以下内容不匹配: Cakeman,哦,我喜欢cakeboy。 这是否超出了正则表达式的范围,或者是否有一种方法可以动态地做到这一点?很难判断您正在尝试做什么,但据我所知,您

我希望正则表达式接受任意数量的字符,然后记住这组字符,然后在后面的行中查找它

例如,如果正则表达式看到行以“TheseCharacters”开头,那么如果它看到“TheseCharacters”出现在行的后面,我希望它与行匹配

示例(所有这些都将匹配):

这些角色,我真的很喜欢这些角色。

Dog1,我最喜欢的单词是Dog1。

以下内容不匹配:

Cakeman,哦,我喜欢cakeboy。


这是否超出了正则表达式的范围,或者是否有一种方法可以动态地做到这一点?

很难判断您正在尝试做什么,但据我所知,您可以使用分组和反向引用来实现这一点。大概是这样的:

<?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]
中可用


如果你不知道你想做什么,这几乎是唯一的办法。其他方法可能是将每个单词匹配或拆分为单个单词,并将其放入一个数组中,然后简单地使用以获得每个单词的计数。

要知道您想做什么有点困难,但据我所知,您可以使用分组和反向引用来完成此操作。大概是这样的:

<?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]
中可用


如果你不知道你想做什么,这几乎是唯一的办法。其他方法可能是将每个单词匹配或拆分成一个数组,然后简单地用它来计算每个单词的数量。

理论科学清楚地表明,使用正则表达式是不可能的。这需要某种内存,所以至少需要一个图灵机而不是有限状态机。您描述的问题是一个非常规问题,它太复杂了,无法用常规语言解决。这是可以证明的,没有办法解决。你可以通过分组和反向引用来做一些事情。你显然可以应用两个单独的正则表达式:一个是从字符串开始捕获任意子字符串,第二种方法是尝试进一步匹配捕获的子字符串。类似的方法适用于您所讨论的内容:。这只是查找字符串中存在多次的任何字母块。您可以将其锚定到字符串的开头,以仅查找第一个字符块,并且仅当该字符块在字符串的后面存在时才进行匹配。@JonathanKuhn您介意描述一下那里发生了什么吗?“变量”的内容存储在哪里?理论科学清楚地告诉我们,使用正则表达式是不可能的。这需要某种内存,所以至少需要一个图灵机而不是有限状态机。您描述的问题是一个非常规问题,它太复杂了,无法用常规语言解决。这是可以证明的,没有办法解决。你可以通过分组和反向引用来做一些事情。你显然可以应用两个单独的正则表达式:一个是从字符串开始捕获任意子字符串,第二种方法是尝试进一步匹配捕获的子字符串。类似的方法适用于您所讨论的内容:。这只是查找字符串中存在多次的任何字母块。您可以将其锚定到字符串的开头,以仅查找第一个字符块,并且仅当该字符块在字符串的后面存在时才进行匹配。@JonathanKuhn您介意描述一下那里发生了什么吗?“变量”的内容存储在哪里?