来自一些 XML 数据:
<Row A1:Height="17" ss:StyleID="s139">
<Cell ss:StyleID="s69"><Data ss:Type="String">Title1</Data><NamedCell
ss:Name="Print_Titles"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell
ss:Name="Print_Area"/></Cell>
<Cell ss:StyleID="s70"><Data ss:Type="String">Title2</Data><NamedCell
ss:Name="Print_Titles"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell
ss:Name="Print_Area"/></Cell>
</Row>
<Row A2:Height="17" ss:StyleID="s139">
<Cell ss:StyleID="s69"><Data ss:Type="String">Title1</Data><NamedCell
ss:Name="Print_Titles"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell
ss:Name="Print_Area"/></Cell>
<Cell ss:StyleID="s70"><Data ss:Type="String">Title2</Data><NamedCell
ss:Name="Print_Titles"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell
ss:Name="Print_Area"/></Cell>
</Row>
....
使用正则表达式,我试图只找到包含“Title1”和“Title2”单元格的第一行。
-(NSString *)extractDataFromXMLString:(NSString *)xmlString{
NSString *headerPattern =
@" *<Row[^>]*>\n"
@" *<Cell[^>]*>.*Title1.*</Cell>\n"
@" *<Cell[^>]*>.*Title2.*</Cell>\n"
@" *</Row>";
NSError *error = NULL;
NSRegularExpression *headerExpression = [NSRegularExpression
regularExpressionWithPattern:headerPattern
options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:&error];
NSRange rangeOfFirstMatch = [headerExpression rangeOfFirstMatchInString:xmlString options:0 range:NSMakeRange(0, [xmlString length])]
return [xmlString substringWithRange:rangeOfFirstMatch]
}
但是我返回了整个字符串有人可以帮助我吗?