Regular expression to split greedy but keep split token?
-
27-02-2021 - |
Question
I have a string like:
{A}{B}={C}{D}<{E}{F}<=
What I want to do is split that string using a regular expression so as to get something like:
1: {A}{B}=
2: {C}{D}<
3: {E}{F}<=
I'm currently splitting the string using (?<=\>)|(?<=\<\=)|(?<=\>\=)|(?<=\=)|(?<=\<)|(?<=!\=)
but it's not producing the desired result, as you can see:
1: {A}{B}=
2: {C}{D}<
3: {E}{F}<
4: =
What do I need to change in the regular expression to get the result I'm looking for?
Solution
Try this instead:
(?<=<=)|(?<=>=)|(?<=!=)|(?<==(?!=))|(?<=<(?!=))|(?<=>(?!=))
Or use match instead of split with this simpler regular expression:
.*?(?:<=|>=|!=|=|<|>)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow