使用正则表达式匹配给定单词中的子字符串(撇号)

时间:2014-10-17 17:28:01

标签: php regex

我有一个服务器应用程序,用俄语单词查找压力。最终用户写了一个单词 жажда 。服务器从另一个服务器下载一个页面,其中包含每个案例/变量的撇号所指示的压力,如 жа'жда 。我需要在下载的页面中找到该单词。

在俄语中,压力总是在元音之后写出来。到目前为止,我一直在使用正则表达式,它是所有可能组合的组合 (жа'жда|жажда') 。是否有一个更优雅的解决方案只使用正则表达式而不是创建一个PHP脚本来创建所有这些组合?

编辑:

  1. 我有一句话 жажда
  2. 下载的页面包含字符串 жа'жда 。 (请注意 撇号,我不知道撇号在哪里 字是)
  3. 我想将这个词与撇号( жа'жда )相匹配。
  4. PS:到目前为止,我有一个PHP脚本创建了正则表达式中使用的字符串 (жа'жда|жажда') (撇号仅在元音之后)与之匹配。我的目标是摆脱这个脚本,并在可能的情况下使用正则表达式。

3 个答案:

答案 0 :(得分:0)

如果我理解你的问题, 有这些选择(d' isder | di' sorder | dis' order | diso' rder | dis' der | disord' er | disde | r | disorder ')其中一个在下载的页面中,我需要找出它是哪个 这可能适合您的需求:

<pre>
<?php
$s = "d'isorder|di'sorder|dis'order|diso'rder|disor'der|disord'er|disorde'r|disorder'|disorde'";
$s = explode("|",$s);
print_r($s);
$matches = preg_grep("@[aeiou]'@", $s);
print_r($matches);  

运行示例:https://eval.in/207282

答案 1 :(得分:0)

嗯......这对你好吗?

<?php
function find_stresses($word, $haystack) {
    $pattern = preg_replace('/[aeiou]/', '\0\'?', $word);
    $pattern = "/\b$pattern\b/";
    // word = 'disorder', pattern = "diso'?rde'?r"
    preg_match_all($pattern, $haystack, $matches);
    return $matches[0];
}

$hay = "something diso'rder somethingelse";
find_stresses('disorder', $hay);
// => array(diso'rder)

您没有指定是否可以有多个匹配,但如果没有,您可以使用preg_match代替preg_match_all(更快)。例如,在意大利语中,我们有àncoraancòra:P

显然,如果使用preg_match,结果将是字符串而不是数组。

答案 2 :(得分:0)

基于您的代码,以及不排除任何功能和无序的要求。我想这就是你想要的。我添加了一个测试向量。

<pre>
<?php
// test code
$downloadedPage = "
there is some disorde'r
there is some disord'er in the example
there is some di'sorder in the example
there also' is some order in the example
there is some disorder in the example
there is some dso'rder in the example
";

$word = 'disorder';
preg_match_all("#".preg_replace("#[aeiou]#", "$0'?", $word)."#iu"
    , $downloadedPage
    , $result
);
print_r($result);
$result = preg_grep("#'#"
    , $result[0]
);
print_r($result);

// the code you need
$word = 'also';
preg_match("#".preg_replace("#[aeiou]#", "$0'?", $word)."#iu"
    , $downloadedPage
    , $result
);
print_r($result);
$result = preg_grep("#'#"
    , $result
);
print_r($result);

工作演示:https://eval.in/207312

相关问题