2017-04-20 106 views
0

我有一个日志文件,其内容如下所示。我试图提取xml分割,匹配少数itemnumber让我们说6654721,6654722和6654725.预期的输出是完整的xml分割与这三个itemnumber匹配。我尝试使用正则表达式(<Record>.*? </Record>),它正好找到每个xml分段,然后尝试应用像(<Record>.*?(6654721|6654722|6654725).*?</Record>)这样的过滤器,但这不像预期的那样工作。你能帮我解决这个问题吗?感谢您在advanace的回应。Notepad ++ reg表达式从日志文件中提取xml消息

2017-04-20 some log file 
2017-04-20 some log file 
2017-04-20 some log file 
<Record> 
    <itemname>Lego Fire Rescue</itemname> 
    <itemnumber>6654721</itemnumber> 
    <availableinv>19</availableinv> 
    <ageplus>3</ageplus> 
    <storeId>19</storeId> 
</Record> 
2017-04-20 some log file 
2017-04-20 some log file 
2017-04-20 some log file 
<Record> 
    <itemname>Lego Fire Rescue</itemname> 
    <itemnumber>6654722</itemnumber> 
    <availableinv>19</availableinv> 
    <ageplus>3</ageplus> 
    <storeId>19</storeId> 
</Record> 
2017-04-20 some log file 
2017-04-20 some log file 
2017-04-20 some log file 
<Record> 
    <itemname>Lego Fire Rescue</itemname> 
    <itemnumber>6654723</itemnumber> 
    <availableinv>19</availableinv> 
    <ageplus>3</ageplus> 
    <storeId>19</storeId> 
</Record> 
2017-04-20 some log file 
2017-04-20 some log file 
2017-04-20 some log file 
<Record> 
    <itemname>Lego Fire Rescue</itemname> 
    <itemnumber>6654725</itemnumber> 
    <availableinv>19</availableinv> 
    <ageplus>3</ageplus> 
    <storeId>19</storeId> 
</Record> 

回答

1

此正则表达式做这项工作:

<Record[^>]*>(?:(?!</Record>).)*\b(?:6654721|6654722|6654725)\b.*?</Record> 

说明:

<Record[^>]>  : '<Record>' with optional attributes 
(?:     : start non capture group 
    (?!    : start negative lookahead, make sure we have not the following 
     </Record> : literally '</Record>' 
    )    : end lookahead 
    .    : any character 
)*     : repeat the non capture group, at this place we are sure we have not </Record> 
\b     : word boundary 
(?:     : non capture group 
    6654721   : 6654721 
    |    : OR 
    6654722   : 6654722 
    |    : OR 
    6654725   : 6654725 
)     : end group 
\b     : word boundary 
.*?     : 0 or more any character, non greedy 
</Record>   : literally '</Record>' 
+0

大,这完美的作品。正是我所期待的。赞赏。如果标签中的某些标签具有像这样的属性,是否可以帮助我使其工作。我仍然希望看到相同的回应。 – Ponns

+2

如果这个答案解决了你的问题,你应该接受它。 – Pharaoh

+0

@Ponn:看我的编辑。 – Toto