2014-10-17 51 views
2

我有一個服務器應用程序,它查找俄語單詞中的壓力。最終用戶寫一個字жажда。服務器從另一個服務器下載頁面,其中包含指示的壓力對於每種情況/ declension像這樣жа'жда。我需要在下載的頁面中找到該字。使用正則表達式匹配給定單詞中的子字符串(撇號)

在俄語中,壓力總是寫在元音之後。我一直在使用一個正則表達式,它是所有可能的組合的組合(жа'жда|жажда')。有沒有更優雅的解決方案只使用正則表達式模式而不是創建一個PHP腳本創建所有這些組合?

編輯:

  1. 我有一句話жажда
  2. 下載的頁面包含字符串жа'жда。 (注意 撇號,以前我不手知道在 字撇號)
  3. 我想與撇號字(жа'жда)相匹配。

P.S:到目前爲止,我有一個PHP腳本創建的字符串(жа'жда|жажда')在正則表達式中使用(單引號只有元音後),這與它匹配。我的目標是擺脫這個腳本,並使用正則表達式,以防萬一。

+0

你在一個時間匹配一個字?或者它是在一個更長的字符串? – Sam 2014-10-17 17:39:42

+0

你只是試圖用相同的元音替換給定字符串中的每個元音,後面跟着使用PHP的撇號? – Jerome 2014-10-17 17:40:59

+0

@Sam是的,一次一個字。 – AMDcze 2014-10-17 17:43:18

回答

0

如果我明白你的問題, 有這些選項(D'isorder | di'sorder | dis'order | diso'rder | disor'der | disord'er | disorde'r |症「)和一個這些是在下載頁面,我需要找出它是一個 這可能會滿足您的需求:

<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

+0

但這個詞是基於用戶的輸入,我必須有一個創建$ s的功能。我已經有了這個,它的工作原理,但我看,只要我可以用正則表達式,而不是額外的PHP腳本: 'function possibleOptions($ word){...}' 'preg_match(「 (「.possibleOptions($ _ POST ['givenWord'])。」)#iu「,$ downloadedPage,$ result)); ' '//正則表達式爲「#(d'isorder | di'sorder | dis'order | diso'rder | disor'der | disord'er | disorde'r | disorder')#iu」' – AMDcze 2014-10-17 18:36:01

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,結果將是字符串而不是數組。

+0

Since你的代碼與@PaulH基本相同相同的答案:除了不包含任何撇號的一個結果是(搜索的字符串必須包含撇號)。但我的問題是,如果我可以用只是一個正則表達式模式來做到這一點(我忘了寫我只是感興趣的是看看我是否可以只使用正則表達式模式而不依賴任何函數)。 – AMDcze 2014-10-17 19:12:31

+0

由於搜索的動態性質(用戶插入的單詞),因此無法單獨使用正則表達式。動態問題=>動態模式 – Iazel 2014-10-17 19:27:48

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

+0

除了不包含任何撇號的一個結果是(搜索的字符串必須包含撇號)。但我的問題是,如果我可以用只是一個正則表達式模式來做到這一點(我忘了寫我只是感興趣的是看看我是否可以只使用正則表達式模式而不依賴任何函數)。 – AMDcze 2014-10-17 19:11:41

+0

代碼適用於匹配沒有函數調用和排除紊亂的要求 – PaulH 2014-10-17 19:36:44