[Closed] Literalise a String
I need to pass a string and convert any ” to ” but I can’t just do a replace ” ” as there might already be a ” in the string.
Can’t find a purely maxscript way to do it so I’m looking into Regex but finding I’m getting stuck trying to match the first quotation mark.
Here is an example text that needs to catch the quotes around apples but not around Mince.
“apples”
“Mince”
Regex: ([^\])[“]
gets the s” but not the first one…
Any ideas?
Hmmm now I need to make it even cleverer…
An odd number of backslashes before a ” is ok… an even number is not…
Where I’m up to…
a = "\\\"Test\\""
fn checkString str =
(
rx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<!\\\\)\""
while (rx.isMatch str) do
str = rx.Replace str "\\\""
return str
)
checkString a
Not pretty but it’ll catch most practical cases…
a = "\\\"Test\\\\\"dshjhsd\""
fn checkString str =
(
rx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<!\\\\)\""
while (rx.isMatch str) do
str = rx.Replace str "\\\""
rx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<!\\\\)(\\\\{2}\")"
while (rx.isMatch str) do
str = rx.Replace str "\\\\\\\""
rx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<!\\\\)(\\\\{4}\")"
while (rx.isMatch str) do
str = rx.Replace str "\\\\\\\\\\\""
rx = dotnetobject "System.Text.RegularExpressions.Regex" "(?<!\\\\)(\\\\{6}\")"
while (rx.isMatch str) do
str = rx.Replace str "\\\\\\\\\\\\\\\""
return str
)
checkString a
That’s going to take some digesting to understand… I love the theory of Regex, but reading it is like reading Russian for me! Thanks again!
((?>\\|")+(?>\\|"))
--- or not grouped result
(?>\\|")+(?>\\|")
hope it help, it return 2 group in regex result so you can had the result out, or you can do another.