Reputation: 2660
I am looking to split my string on basis of [, ]. But there is one restriction, if the [, ] comes within " ", we should ignore them .
Example :
For a string:
"""hello "hi" [abc: "pqr[abc]"] bye | [xyz]"""
the splitted strings should be :
hello "hi"
abc: "pqr[abc]"
bye |
xyz
Upvotes: 0
Views: 67
Reputation: 350760
If you really need to split, then you can use look-ahead to check that the number of quotes that follows the candidate delimiter is even:
[\][](?=[^"]*(?:"[^"]*"[^"]*)*$)
But look-ahead is quite inefficient. If it is acceptable to do the opposite and find the matches between the delimiters (with find()
), instead of splitting, then you can use:
Upvotes: 1
Reputation: 2788
One of possible solutions (which doesn't require tricky regexps) might be as follows:
(1) split without regard to quotes
(2) count (cumulative) numbers of quotes at each resulting component
(3) re-join at boundaries where such a number is odd
Upvotes: 1
Reputation: 89574
Instead of trying to split, you can use the find()
method and describe all that isn't a square bracket except for parts between quotes:
[^\]\["]+(?:"[^"]*"[^\]\["]*)*|(?:"[^"]*"[^\]\["]*)+
(Dont't forget to add backslashes in your pattern string.)
Upvotes: 1