获取正则表达式匹配后的上一个和下一个10个单词



我正在尝试获取匹配后的前10个单词和下10个单词

PHP 中的我的Regex

(?:S+s*){0,5}S*“your age”S*(?:s*S+){0,10}

第一段匹配

如果您自输入的出生日期起已满28年,则数字28将显示在该文本框中。在标题为"你的年龄"的文本框旁边,你会看到另一个标题为"you Born At"的文本盒。此框将向您显示出生日期的详细信息,包括您出生的日期

"你的年龄">的10个单词前后我想要什么

显示在该文本框中。在标题为"你的年龄"的文本框旁边,你会看到另一个标题为"you Born At"的文本盒。

这里有一个使用preg_replace:的选项

$input = "if you have completed 28 years since the entered date of birth, the number 28 would be shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”. This box would show you clear details of your birth date including the day on which you were born";
$match = preg_replace("/^.*?((?:S+s+){10}“your age”(?:S+s+){10}).*$/", "$1", $input);
echo $match;

此打印:

shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born 

我使用以下模式来匹配整个字符串,同时捕获your age术语及其两侧的10个单词:

^.*?((?:S+s+){10}“your age”(?:S+s+){10}).*$

@TimBiegeleisen谢谢,我还找到了另一个解决方案,实际上我更快。

(?:S+s){0,10}S*“your age”S*(?:sS+){0,10}

对于我的代码

$input = "if you have completed 28 years since the entered date of birth, the number 28 would be shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”. This box would show you clear details of your birth date including the day on which you were born";
$match = preg_replace("/(?:S+s){0,10}S*“your age”S*(?:sS+){0,10}", $input, $match);
echo $match[0];

这将打印

shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”.

相关内容

最新更新