0

NSRegularExpression を使用して、xml からタグ間のデータを取得したい

これはxmlです

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="@link" xmlns:xsi="@link" xsi:schemaLocation="@link" version="1.0">
<field left="493" top="670" right="1550" bottom="760" type="text">
<value encoding="utf-16">JENNIFER mml</value>
<line left="493" top="670" right="1550" bottom="733">
<char left="493" top="670" right="549" bottom="733" confidence="69">J</char>
<char left="565" top="670" right="605" bottom="718" confidence="71" suspicious="true">E</char>
<char left="623" top="670" right="660" bottom="718" confidence="76">N</char>
<char left="678" top="670" right="720" bottom="722" confidence="56">N</char>
<char left="736" top="674" right="776" bottom="730" confidence="80">I</char>
<char left="804" top="674" right="841" bottom="729" confidence="74">F</char>
<char left="858" top="670" right="902" bottom="725" confidence="80">E</char>
<char left="922" top="670" right="964" bottom="730" confidence="86">R</char>
<char left="965" top="670" right="1442" bottom="730" confidence="100" />
<char left="1443" top="685" right="1495" bottom="720" confidence="2" suspicious="true">m</char>
<char left="1492" top="685" right="1534" bottom="719" confidence="11" suspicious="true">m</char>
<char left="1544" top="685" right="1550" bottom="718" confidence="100" suspicious="true">l</char>
</line>
</field>
</document>

このデータを値タグの間で抽出したい

<value encoding="utf-16">JENNIFER mml</value>

これはiosコードです

 NSString *xml =@"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><document xmlns=\"@link\" xmlns:xsi=\"@link\" xsi:schemaLocation=\"@link\" version=\"1.0\"><field left=\"493\" top=\"670\" right=\"1550\" bottom=\"760\" type=\"text\"><value encoding=\"utf-16\">JENNIFER mml</value><line left=\"493\" top=\"670\" right=\"1550\" bottom=\"733\"><char left=\"493\" top=\"670\" right=\"549\" bottom=\"733\" confidence=\"69\">J</char><char left=\"565\" top=\"670\" right=\"605\" bottom=\"718\" confidence=\"71\" suspicious=\"true\">E</char><char left=\"623\" top=\"670\" right=\"660\" bottom=\"718\" confidence=\"76\">N</char><char left=\"678\" top=\"670\" right=\"720\" bottom=\"722\" confidence=\"56\">N</char><char left=\"736\" top=\"674\" right=\"776\" bottom=\"730\" confidence=\"80\">I</char><char left=\"804\" top=\"674\" right=\"841\" bottom=\"729\" confidence=\"74\">F</char><char left=\"858\" top=\"670\" right=\"902\" bottom=\"725\" confidence=\"80\">E</char><char left=\"922\" top=\"670\" right=\"964\" bottom=\"730\" confidence=\"86\">R</char><char left=\"965\" top=\"670\" right=\"1442\" bottom=\"730\" confidence=\"100\"> </char><char left=\"1443\" top=\"685\" right=\"1495\" bottom=\"720\" confidence=\"2\" suspicious=\"true\">m</char><char left=\"1492\" top=\"685\" right=\"1534\" bottom=\"719\" confidence=\"11\" suspicious=\"true\">m</char><char left=\"1544\" top=\"685\" right=\"1550\" bottom=\"718\" confidence=\"100\" suspicious=\"true\">l</char></line></field></document>";
NSString *pattern = @"<value>(\\d+)</value>";

NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:pattern
                              options:NSRegularExpressionCaseInsensitive
                              error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:xml options:0 range:NSMakeRange(0, xml.length)];

NSRange matchRange = [textCheckingResult rangeAtIndex:1];
NSString *match = [xml substringWithRange:matchRange];
NSLog(@"Found string '%@'", match);
4

1 に答える 1

1

現在の正規表現は、正確な<value>タグ数字のみに一致し\d+ます。

<value>(\d+)</value>

ただし、入力には属性 ( encoding="utf-16")があり、値として数値が含まれていません ( JENNIFER mml):

<value encoding="utf-16">JENNIFER mml</value>

最初の問題を克服するには、属性を正​​規表現にハードコーディングするか、パターンを少し変更します。

<value encoding="utf-16">
or
<value[^>]*>

タグの値を一致させるには、アルファベット順 (空白あり) のように見えますが、数字も挿入します。次のように使用できます。

[a-zA-Z0-9\s]+

したがって、完全に試すことができます:

<value[^>]*>([a-zA-Z0-9\s]+)</value>

現在のコードで(コピー+貼り付け用):

NSString *pattern = @"<value[^>]*>([a-zA-Z0-9\\s]+)</value>";

更新(の間は何でも一致<value></value>)
コメントごとに、<value></value>タグ間の正確なテキストには、英数字だけでなく、任意の文字を含めることができます。これを処理するには、すべてを次のように一致させることができます(.*):

<value>[^>]*>(.*)</value>

あなたの現在のコードで:

NSString *pattern = @"<value[^>]*>(.*)</value>";
于 2012-11-08T07:01:05.517 に答える