yaml / yamlscript

Programming in YAML
MIT License
382 stars 31 forks source link

Alternate regex syntax #114

Closed ingydotnet closed 3 months ago

ingydotnet commented 6 months ago

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.

xanni commented 6 months ago

Yes, I also like \r"..."

mevanlc commented 3 months ago

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!

ingydotnet commented 3 months ago

@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.

mevanlc commented 3 months ago

@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)?

ingydotnet commented 3 months ago

@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.