简体   繁体   English

如何使用正则表达式识别重复类型的模式 (c#)

[英]How to identify a repetitive type of pattern with regex (c#)

Currently I'm doing:目前我正在做:

Regex.Match(ln, "\"[\\u0000-\\u007E]+\"")

The problem is if ln is like "" text... "" it won't work as wanted.问题是如果ln类似于 "" text... "",它将无法正常工作。

Same to """ text... """, etc...与“””文本相同... “””等...

How can I make the " " repetitive?我怎样才能使“”重复?

It should also match to " text1 " text2 " text3 ".它还应匹配“ text1text2text3 ”。

The point is, it should match with the most 2 outer " " if the number of " is par or cut the last " if number impar.关键是,如果"的数量是标准的,它应该与最外面的2个" "匹配,或者如果数量不正常,则删除最后一个"。

Ex: "stack"overflow" -> match "stack"例如: "stack"overflow" -> 匹配"stack"

Ex: "stack""overflow" -> match "stack""overflow"例如: "stack""overflow" ->匹配"stack""overflow"

Ex: text1""text2""text3 -> match ""text2""例如: text1""text2""text3 -> 匹配""text2""

Ex: "text1"text2"text3 -> match "text1"例如: "text1"text2"text3 -> 匹配"text1"

Any idea how to make this pattern?知道如何制作这种图案吗?

An idea is to work with negated " between.一个想法是在否定"之间工作。

  • Match "[^"]*"匹配"[^"]*"
  • Repeat [^"]*"[^"]*" any amount of times重复[^"]*"[^"]*"任意次数
"[^"]*"(?:[^"]*"[^"]*")*

See this demo at regex101 (the \n in multiline demo is for staying in line)请参阅 regex101 上的此演示(多行演示中的\n用于保持排队)

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

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