我正在解析日志文件并尝试匹配错误语句。我匹配“错误 CS”的行的一部分将适用于许多行,有些重复,有些不重复。有没有办法我不能返回重复项。使用 Java 风格的 RegEx..
示例:我的简单正则表达式返回
Class1.cs(16,27): error CS0117: 'string' does not contain a definition for 'empty'
Class1.cs(34,20): error CS0103: The name 'thiswworked' does not exist in the current context
Class1.cs(16,27): error CS0117: 'string' does not contain a definition for 'empty'
Class1.cs(34,20): error CS0103: The name 'thiswworked' does not exist in the current context
希望它返回:
Class1.cs(16,27): error CS0117: 'string' does not contain a definition for 'empty'
Class1.cs(34,20): error CS0103: The name 'thiswworked' does not exist in the current context
最佳答案
一种解决方案是使用您的正则表达式进行匹配,然后将该行放入数据结构中,例如 set处理为您删除重复项。在解析结束时,只需打印集合的内容。
如果您担心顺序,您可以将行作为键并以行号作为值添加到某种 map (可能在插入之前检查匹配的条目)。如果按值排序,您将获得给定行的第一个实例的列表。
关于java - 正则表达式在模式匹配时返回唯一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3771242/