添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

"Field1","Field2","item1,item2,item3","Hello,""John"""

The one thing I understand is it is splitting the string on , but anything after that I am not sure.

If anyone can explain this Regex please.

If you can dissect it to the simplest possible level, I would appreciate it.

, -> match literal comma
(?=...) -> positive lookahead
[^"]*" -> match anything before a " followed by a literal "
[^"]*"[^"]*" -> match a pair of above 
(?:[^"]*"[^"]*")* -> Match 0 or more of pairs (0, 2, 4, 6 sets)
[^"]*$ -> Followed by any non-quote till end of string

Example Input:

"Field1,Field2","Field3","item1,item2,item3"
  • First it will match , before "Field3" because lookahead: (?=(?:[^"]*"[^"]*")*[^"]*$) is making sure there are 4 double quotes after this comma.
  • Second it will match , after "Field3" because lookahead: (?=(?:[^"]*"[^"]*")*[^"]*$) is making sure there are 2 double quotes after this comma.
  • It is not matching comma between Field1 and Field2 because # of quotes after that are odd in numbers and hence lookahead (?=(?:[^"]*"[^"]*")*[^"]*$) will fail.
  • I understand that part but need to understand it part by part so I can create something similar myself. – gbs Nov 7, 2014 at 21:09 Thanks got little more understanding but would it be possible to explain a bit more using the example string I have above. – gbs Nov 7, 2014 at 21:26 ok see this demo link with above regex and a simplified example. I will add more explanation – anubhava Nov 7, 2014 at 21:32 Now your initial explanation steps makes more sense to me. The demo link was helpful. Thanks. – gbs Nov 7, 2014 at 22:00 Thanks...I understand that part but need to understand it part by part so I can create something similar myself. – gbs Nov 7, 2014 at 21:09 @gbs if a , is in between " and " it will have odd number of " ahead of it.If it is outside it will have even number of " ahead of it.hope that helps – vks Nov 7, 2014 at 21:14

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.