0

I build some kind of editor, so my text is being split by that regex:

str.split("((?<=[a-zA-Z0-9={}])\\s(?=[a-zA-Z0-9={}(]))|[\\n\\r]+|((?<=[{}])|(?=[{}]))|((?<=[=])|(?=[=]))");

It works really well, for example I can write this code:

int x = 5
char y = 3
while x + y > 2 {
    cmd1 50 20
    cmd3 40 30
}

it splits it like that:

[int, x, , =, 5, char, y, , =, 3, while, x + y > 2, , {,    cmd1, 50, 20,   cmd3, 40, 30, , }]

( I ignore white-spaces so it's okay )

but when I write for example:

print "hello"
print "x+2 / 5 = 3"

that's the result:

[print "hello", print "x+2 / 5, , =, 3"]

so the first one should be [print,"hello"] and the second one should be the same, I mean it should be [print, "x+2/5=3"] but it sadly not the case as you can see.

How could I make my regex to ignore its rules when he meets a quotation mark block? and being split by "{TEXT}"?

Thanks.

EDIT:

@WiktorStribiżew made a good fix: https://regex101.com/r/ZsHf46/1 but it doesn't split the words from the comma, for example

print "hello" should be: [print, "hello"] and not [print "hello"]

how can I fix that?

Dorki
  • 1,021
  • 2
  • 8
  • 23

0 Answers0