RegEx Help [Kem?!]

I have 6 RegEx statements… that all “kinda” work

  • normal
    rule: “(.*$)”

  • numbers
    rule: “([-+]?[0-9]*\.?[0-9])”

  • keywords
    rule: “(?<!\B)(test|stuff)\b”

  • operators
    rule: “(<=|>=|!=|=|>|<|\+|-|\*|\^|%|/)”

  • strings
    rule: “/([”])((\\{2})|(.?^\\*))\1/"

  • comment
    rule: “(//|\’).*$”

The problem is, all 6 of these rules are applied to a block of text in the order above …
the idea being numbers would get an attribute, but then if those numbers are inside of QUOTE they would become a string , and lastly if a comment marker was found, the rest of the line would be colored as a comment regardless of the rest

But the String and Comment rules are not working

Note : the \ are doubled as a Swift requirement (escaped) ,

examples of something that would be valid for these would help
fwiw I dont think kem reads this

these “seem” to do the right thing

string : “([^”]|"")*"
" followed by anything with " doubled up ending in a "

comment : (//|\’).*$
// or ’ followed by anything until the end of line

TEST is a test keyword, which should be blue when NOT in a string or comment

the entire first line should be PURPLE including the word TEST
the entire 2nd line should be RED , comment is not going to EOL
same with 3rd line comments are either // or ’
on the 4th line, the 1234 and test should be orange and blue, except for the ones following the // those should be red

ok… solved part of it …

the comment rule was wrong… this works

but the quoted string rule is messed up

I think the $ should come at the end of the search pattern (if at all).


looks like I found a quoted string pattern… no idea where I came up with the one above.

"(\"(\"\"|.)+\")" seems to work

C-8DO… are you talking about the “normal” pattern… the $ IS at the end… for some reason it seems Swift requires the pattern in ()

Ah, my bad. I tested it in Kem’s RegExRX and there it matched the quoted string with the $ at the end…

No worries… my biggest issue getting these to work right was when to escape things

my app crashes unless they are in ( regex )

sample data

"quoted test string"
this is a // comment "string"
this is a "string "" with a string" and something after
' this is the other comment
and this is a ' comment as well for the last half

had to modify the string one (unless you want to permit newline and carriage returns in a string ?)

string : “([^”\n\r]|”")*"
" followed by anything just not an embedded newline , carriage return and " has to be doubled up and then "

comment : (//|\’).*$
// or ’ followed by anything until the end of line