yamlscript
yamlscript copied to clipboard
Alternate regex syntax
We have /^.*\/foo\/bar.*$/
now which leads to leaning toothpick syndrome.
Clojure uses #"^.*/foo/bar.*$"
.
We can't use that because it would be a yaml comment a most of the time.
We can use \#"^.*/foo/bar.*$"
though.
We could also use \"^.*/foo/bar.*$"
.
Python has ``r"^./foo/bar.$"so we could use
\r"^./foo/bar.$"`.
I like \r"..."
because we could later make other kinds of strings with other letters.
Yes, I also like \r"..."
A bit off-topic because it's not YAML-related, but I -- also thinking of toothpick syndrome -- recently did a ranking of possible characters for an alternate (second) regex delimiter for GitHub search. (Btw, did you know you can use regex in GitHub search? It's neat!). https://github.com/orgs/community/discussions/114708
Since people who know about toothpick syndrome and care enough about it to write posts about it, are possibly few and far between, I thought I'd risk a little off-topicness to say hi here anyway!
@mevanlc Hi!
We resolved this quite nicely with rx('^.*/.*\.yaml$')
.
Where rx
is a stand library function we just added.
Also there already was one that did the same thing called re-pattern
.
Both generate a regex pattern object from a string.
@ingydotnet That's a nice, brief syntax! Thanks for sharing -- I will be trying it out. How would an internal literal '
be escaped in this case? Or would you need to use an alternate quoting scheme for that (ala bash)?
@mevanlc (msg =~ rx('escaping single quotes shouldn''t be a problem'))
:)
YAML uses ''
to escape single quotes in single quoted strings.
The beauty of that choice is that there is only only character to escape. (\'
would require \\
).
YS does the same thing at the expression level.