News for hackers. Stuff that matters.

Best syntax for a regex literal

I am working on a programming language.

What is the best syntax for regular expression literal?

`(...)

re (...)

$(...)

@(...)

<...>

/.../

(re:...)

`...`

( 4 comments | on HN )
Re:Best syntax for a regex literal by zenithcobra on Tuesday January 29, 2019 @02:06PM (#19028984)
"^[AEIOU]{1}.+[aeiou]{1}$" or `(^[AEIOU]{1}.+[aeiou]{1}$) or re (^[AEIOU]{1}.+[aeiou]{1}$) or @(^[AEIOU]{1}.+[aeiou]{1}$) or <^[AEIOU]{1}.+[aeiou]{1}$> or /^[AEIOU]{1}.+[aeiou]{1}$/ or (re:^[AEIOU]{1}.+[aeiou]{1}$) or `^[AEIOU]{1}.+[aeiou]{1}$`

all look ugly. I would either use a triple distinct character set like python with """^[AEIOU]{1}.+[aeiou]{1}$""" or ~~~^[AEIOU]{1}.+[aeiou]{1}$~~~ but for simplicity you should use the re(...) one because you can intuitively know where the wrapper starts and ends.

Re:Best syntax for a regex literal by vonox on Tuesday January 29, 2019 @02:12PM (#19029023)
Thanks
Re:Best syntax for a regex literal by vonox on Tuesday January 29, 2019 @02:14PM (#19029033)
I also use an alternative regex syntax to improve readability. For example:

re ("abc" | ["0"-"9"]+)

means this:

/abc|[0-9]+/

(You can put whitespaceses into the literal)

Re:Best syntax for a regex literal by ktpsns on Tuesday January 29, 2019 @04:12PM (#19029819)
PCRE have the /x modifier to allow whitespace and comments.

When reinventing the wheel, it's hard to top what other do already pretty well. Also, be warned people have to learn your syntax and this will probably stop them using it.