2

私は次のことを行う正規表現を構築しようとしています:

xml タグで囲まれた「alphabet」という単語を探します。検索は次のように一致します。

<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 つの特殊文字で区切ることができます: " * `、検索では大文字と小文字を区別する必要があります。特殊文字の有無にかかわらず、アルファベットという単語を具体的に検索する正規表現を作成してください。上記の通り。

4

3 に答える 3

2

これは、正規表現を xml/html などの解析に使用すべきではないという警告で機能します。

単純なサンプルをキャプチャしてから、コールバックでサブプロセスする方が常に簡単です。
この場合、([alphabet"*`,]+) をキャプチャし、不要な文字を取り除き、比較を行います。

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";
  }

?>
于 2011-12-27T17:44:22.593 に答える
1

あなたはこれを試すことができます

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

またはこれ

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

編集

エスケープするのを忘れてすみません*

于 2011-12-27T15:16:23.997 に答える
0

あなたがリストしたケースで機能するものを私が手に入れました:

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

http://www.rubular.com/をチェックしてください。正規表現のライブ テストがあります。

于 2011-12-27T15:19:14.313 に答える