.net regex - 最后一个 `<item>` 前不包含句号的字符串 - 尝试 2 [英] .net regex - strings that don't contain full stop preceding last `<item>` - Attempt 2
问题描述
这个问题来自.net regex - 最后一个列表项不包含句号的字符串
问题现在如下.请注意,示例已被修改和添加更多 - 所有这些都需要满足.好的例子不应该返回匹配,坏的例子应该返回匹配.
Problem is now the below. Note that examples have been amended and more added - all need to be satisfied. Good examples should return no matches, and bad examples should return matches.
我正在尝试使用 .net regex 来识别 XML 数据中最后一个标记前不包含句号的字符串.我对正则表达式没有太多经验.我不确定我需要改变什么&为什么要得到我正在寻找的结果.
I'm trying to use .net regex for identifying strings in XML data that don't contain a full stop before the last tag. I have not much experience with regex. I'm not sure what I need to change & why to get the result I'm looking for.
数据中每行末尾有换行符和回车符.
There are line breaks and carriage returns at end of each line in the data.
架构用于 XML.我们无法访问 .Net 代码 - 只有使用自定义构建的应用程序的用户.
A schema is used for the XML. We have no access to .Net code - just users using a custom built application.
错误 XML 数据的示例 1 - 应该给出 1 个匹配:
Example 1 of bad XML Data - should give 1 match:
<randlist prefix="unorder">
<item>abc</item>
<item>abc</item>
<item>abc</item>
</randlist>
错误的 XML 数据的示例 2 - 应该给出 1 个匹配:
Example 2 of bad XML Data - should give 1 match:
<randlist prefix="unorder">
<item>abc. abc</item>
<item>abc. abc</item>
<item>abc. abc</item>
</randlist>
良好 XML 数据的示例 1 - regexp 不应给出匹配项 - 最后一个 </item>
之前的句号:
Example 1 of good XML Data - regexp should give no matches - full stop preceding last </item>
:
<randlist prefix="unorder">
<item>abc</item>
<item>abc</item>
<item>abc.</item>
</randlist>
良好 XML 数据的示例 2 - regexp 不应给出匹配项 - 最后一个 </item>
之前的句号:
Example 2 of good XML Data - regexp should give no matches - full stop preceding last </item>
:
<randlist prefix="unorder">
<item>abc. abc</item>
<item>abc. abc</item>
<item>abc. abc.</item>
</randlist>
我尝试过的 Reg exp 模式不起作用(误报或使用 https://regex101.com/) 针对上述错误 XML 数据中的标准(未在良好 XML 数据上进行测试):
Reg exp patterns I tried that didn't work (either false positives or no matches using https://regex101.com/) for criteria above in the bad XML data (not tested on good XML data):
^<randlist \w*=[\S\s]*\.*[^.]*<\/item>[\n]*<\/randlist>$
^\s+<item>[^<]*?(?<=\.)<\/item>$
推荐答案
下面的正则表达式模式对我们有用 - 在 Notepad++ 中测试
The regexp pattern below works for us - tested in Notepad++
[^.]<\/item>\s{1,2}<\/randlist>
这篇关于.net regex - 最后一个 `<item>` 前不包含句号的字符串 - 尝试 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!