Split a String based on regex

后端 未结 4 860
情歌与酒
情歌与酒 2020-12-17 18:55

I have a string that needs to be split based on the occurrence of a \",\"(comma), but need to ignore any occurrence of it that comes within a pair of parentheses. For exampl

4条回答
  •  隐瞒了意图╮
    2020-12-17 19:08

    Try this

    \w{3}(?=,)|(?<=,)\(\w{3},\w{3}\)(?=,)|(?<=,)\w{3}
    

    Explanation: There are three parts separated by OR (|)

    • \w{3}(?=,) - matches the 3 any alphanumeric character (including underscore) and does the positive look ahead for comma

    • (?<=,)\(\w{3},\w{3}\)(?=,) - matches this pattern (ABC,E4R) and also does a positive lookahead and look behind for the comma

    • (?<=,)\w{3} - matches the 3 any alphanumeric character (including underscore) and does the positive look behind for comma

提交回复
热议问题