简体   繁体   中英

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.

Same to """ text... """, etc...

How can I make the " " repetitive?

It should also match to " text1 " text2 " text3 ".

The point is, it should match with the most 2 outer " " if the number of " is par or cut the last " if number impar.

Ex: "stack"overflow" -> match "stack"

Ex: "stack""overflow" -> match "stack""overflow"

Ex: text1""text2""text3 -> match ""text2""

Ex: "text1"text2"text3 -> match "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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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