2011-12-27 16 views
2

我一直在試圖建立一個正則表達式來做到以下幾點:正則表達式匹配單詞由字符分隔,幷包含由XML標記

查找包含在XML標記單詞「字母」,搜索將匹配如下:

<hw>Al"pha*bet</hw> 
<hw>Al"pha*be`t</hw>   
<hw>alphabet</hw>  
<hw>al*pha*bet</hw>   
<hw>al"pha"b"et</hw> 

這個詞可以用3個特殊字符隔開:「*`,搜索就必須有區分大小寫能否請你幫我通過構建一個將專門搜索單詞字母用正則表達式。或者沒有上面提到的任何特殊字符

+3

你心裏有一個特定的語言?請注意,我的第一個想法是拉動帶標籤的子串,消除這些字符然後匹配,但根據您的語言或數據集可能不理想。 – 2011-12-27 15:13:49

+0

是的..最好的方式去這種取決於什麼語言,以及如何獲取示例數據開始。例如,如果您使用的是XML解析器類(例如php),那麼您可以使用預製函數輕鬆抓取標籤。但即使使用純正則表達式直接抓取原始內容,它仍然是「更清潔」的,去除特殊字符,然後與「字母」進行比較...特別是因爲我有一個偷偷摸摸的「字母」只是一個例子,你會將這個應用到任何單詞... – 2011-12-27 15:33:31

+0

你會匹配的語言字符串?你想匹配什麼:單詞,行,標籤(含內容),還是像上面這樣的完整塊? – Qtax 2011-12-27 15:40:49

回答

1

你可以試試這個

a([`"\*])*l([`"\*])*p([`"\*])*h([`"\*])*a([`"\*])*b([`"\*])*e([`"\*])*t 

或者這

>\s*a([`"\*])*l([`"\*])*p([`"\*])*h([`"\*])*a([`"\*])*b([`"\*])*e([`"\*])*t\s*< 

編輯

對不起忘了逃跑*

+0

'*'在一個字符類中沒有特殊的含義,也不需要任何逃生有爲什麼使用捕獲組到處 – Qtax 2011-12-27 15:34:59

+0

? @Qtax在正則表達式中我並不完美,你可能會提出一個建議 – Oybek 2011-12-27 15:38:23

+0

@Oybek他意味着''''\ *]'可以是'[''*]',因爲'* char類(括號內) – 2011-12-27 15:41:24

0

一個我得到了你的作品中所列的情況:

/<[a-zA-Z]+>al"*\**pha\**\"*b\"*e`*t<\/[a-zA-Z]+>/i 

結賬http://www.rubular.com/。它有一個正則表達式的實時測試。

+0

我認爲你*可能*在解釋問題時太過於字面意思了......這些特殊字符的位置可能只是一些例子並且它們中的任何一個都可以出現在字母之間的任何地方.. – 2011-12-27 15:26:25

+0

我也有一個偷偷摸摸的「字母表」只是一個示例詞,正則表達式將應用於任何單詞...我知道OP沒有說這個,但只是說'... – 2011-12-27 15:34:18

+0

好的,我會嘗試修復;-) – ThatOtherPerson 2011-12-27 15:36:44

2

這將與正則表達式不應該被用來解析XML/HTML等的告誡工作..

它總是更容易捕捉簡單樣本,然後子進程它們的回調。
在這種情況下,捕獲([字母「*`,] +),然後去掉不需要的字符,然後做一個比較。

一個Perl示例,其概念與Perl/PHP/C#等相同。 ..

$sample = ' 
    <hw>Al"pha*bet</hw> 
    <hw>Al"pha*be`t</hw>   
    <hw>alphabet</hw>  
    <hw>al*pha*bet</hw>   
    <hw>al"pha"b"et</hw> 
'; 

$specialword = 'alphabet'; 
$uc_specialword = uc($specialword); 

while ($sample =~ m{<([A-Za-z_:][\w:.-]*)(?:\s+(?:".*?"|\'.*?\'|[^>]*?)+)?\s*(?<!/)>([$specialword"*`,]+)</\1\s*>}isg) 
{ 
    ($matchstr, $checkstr) = ($&, $2); 
    $checkstr =~ s/["*`,]//g; 
    if (uc($checkstr) eq $uc_specialword) { 
     print "Found '$checkstr' in '$matchstr'\n"; 
    } 
} 

擴展正則表達式:

m{ # Regex delim 
<       # Open tag 
    ([A-Za-z_:][\w:.-]*)     # Capture 1, the tag name 
    (?:\s+(?:".*?"|\'.*?\'|[^>]*?)+)?\s* # optional attr/val pairs 
    (?<!/) 
> 
([alphabet"*`,]+)  # Capture 2, class of special characters allowed, 'alphabet' plus "*`, 
</\1\s*>     # Close tag, backref to tag name (group 1) 

}xisg # Regex delim. Options: expanded, case insensitive, single line, global 

輸出:

Found 'Alphabet' in '<hw>Al"pha*bet</hw>' 
Found 'Alphabet' in '<hw>Al"pha*be`t</hw>' 
Found 'alphabet' in '<hw>alphabet</hw>' 
Found 'alphabet' in '<hw>al*pha*bet</hw>' 
Found 'alphabet' in '<hw>al"pha"b"et</hw>' 

PHP示例的

使用preg_match()可以在這裏http://www.ideone.com/8EBpx

<?php 

    $sample = ' 
    <hw>Al"pha*bet</hw> 
    <hw>Al"pha*be`t</hw>   
    <hw>alphabet</hw>  
    <hw>al*pha*bet</hw>   
    <hw>al"pha"b"et</hw> 
    '; 

    $specialword = 'alphabet'; 
    $uc_specialword = strtoupper($specialword); 
    $regex = '~<([A-Za-z_:][\w:.-]*)(?:\s+(?:".*?"|\'.*?\'|[^>]*?)+)?\s*(?<!/)>([' . $specialword. '"*`,]+)</\1\s*>~xis'; 
    $pos = 0; 

    while (preg_match($regex, $sample, $matches, PREG_OFFSET_CAPTURE, $pos)) 
    { 
    $matchstr = $matches[0][0]; 
    $checkstr = $matches[2][0]; 

    $checkstr = preg_replace('/[" * `,]/', "", $checkstr); 
    if (strtoupper($checkstr) == $uc_specialword) 
     print "Found '$checkstr' in '$matchstr'\n"; 

    $pos = $matches[0][1] + strlen($matchstr); 
    } 

?> 

使用preg_match_all()發現可以在這裏找到http://www.ideone.com/C6HeT

<?php 

    $sample = ' 
    <hw>Al"pha*bet</hw> 
    <hw>Al"pha*be`t</hw>   
    <hw>alphabet</hw>  
    <hw>al*pha*bet</hw>   
    <hw>al"pha"b"et</hw> 
    '; 

    $specialword = 'alphabet'; 
    $uc_specialword = strtoupper($specialword); 
    $regex = '~<([A-Za-z_:][\w:.-]*)(?:\s+(?:".*?"|\'.*?\'|[^>]*?)+)?\s*(?<!/)>([' . $specialword. '"*`,]+)</\1\s*>~xis'; 

    preg_match_all($regex, $sample, $matches, PREG_SET_ORDER); 

    foreach ($matches as $match) 
    { 
    $matchstr = $match[0]; 
    $checkstr = $match[2]; 

    $checkstr = preg_replace('/[" * `,]/', "", $checkstr); 
    if (strtoupper($checkstr) == $uc_specialword) 
     print "Found '$checkstr' in '$matchstr'\n"; 
    } 

?> 
+0

$ astring =〜s/[「*' ] // g; $ astring =〜s /&Verbar; // g; if($ astring =〜/ $ pattern/i) 什麼回合呢? $ pattern是「字母表」?是否有更簡單的方法將其轉換爲C或PHP? 非常感謝您的幫助! – Alex 2011-12-27 18:24:55

+0

@Alex - 添加一些php示例。 – sln 2011-12-27 21:54:15

相關問題