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.
–
–
–
–
–
–
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.