简体   繁体   English

正则表达式基于特定模式匹配字符串

[英]Regex match strings based on a certain pattern

How do I match a string using Regex based on a pattern with certain amount of characters to look for?如何根据具有一定数量要查找的字符的模式使用正则表达式匹配字符串?

Example:例子:

|V.2|58|
10001|W20101|W20101|G00001||||學徒劍盾|8|9|768||-1|1||||||||40002||||||1|14||2||40027|40028|40029|40030||2|22|113|||||||||||2|50|100|7|||||
10002|W30101|W30101|G00001||||學徒大斧|9|9|768||-1|1||||||||40003||||||1|17||3||40031|40032|40033|40034||2|26|142|||||||||||2|50|100|9|||||

If I know there's 58 vertical bars on each line then how would I set up a regex to match those strings based on the information I have?如果我知道每行有 58 个竖线,那么我将如何根据我拥有的信息设置正则表达式来匹配这些字符串? And as you also can tell the 58 at the top is the amount of vertical bars there should be in each string.你也可以告诉顶部的 58 是每个字符串中应该有的垂直条的数量。

10479|I00208||G00005||||青鐵礦|29||0||-1|30||||3|||||||||100|5|1||54|$53$原始的礦石,整體泛著鐵青的色澤。

#IMG$NoticeIcon#30~45級的副本掉落或跟公會商人購買。
$7$能與其他材料結合,製作40級的合金材料。
|||||||||||||||||||||||||||

Another example is this.另一个例子是这个。 This one has multiple lines but is still within the 58 vertical bars' range.这一条有多条线,但仍在 58 个竖线的范围内。

Is there a way to match the pattern exactly based on the amount of vertical bars there is?有没有办法根据垂直条的数量完全匹配模式?

Thanks in advance!提前致谢!

To answer your immediate question, yes, you can match this with a regex:要回答您的直接问题,是的,您可以将其与正则表达式匹配:

^[^|]*(?:\|[^|]*){58}*$

when compiled using RegexOptions.Multiline , will match from the first character of a line until exactly 58 bars (and any non-bar characters after that) have been matched.当使用RegexOptions.Multiline编译时,将从一行的第一个字符开始匹配,直到正好匹配 58 个条形(以及之后的任何非条形字符)。

Test it live on regex101.com .在 regex101.com 上进行实时测试。

It's completely context-unaware, though, so it absolutely relies on that number.但是,它完全不了解上下文,因此它绝对依赖于该数字。 In your example file, you'd need to exclude the first line from matching - otherwise those two bars will be included in the first match.在您的示例文件中,您需要从匹配中排除第一行 - 否则这两个条将包含在第一个匹配中。

However, it looks like you're not using the right tool for the job.但是,您似乎没有使用正确的工具来完成这项工作。 Maybe a CSV parser would be better suited?也许 CSV 解析器更适合? It appears your multiline data sample uses a quoting character in the entry that contains newlines, that is something a CSV parser can handle - and it will also handle cases where bars occur in a quoted field, something that this regex can't do.看来您的多行数据样本在包含换行符的条目中使用了引用字符,这是 CSV 解析器可以处理的 - 并且它还将处理在引用字段中出现条形的情况,这是该正则表达式无法做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM