[:0-255:] could be an option for example to write a number range.
Also regex / variable interpolation should be added to regexes in languages probably :)
[:0-255:] could be an option for example to write a number range.
Also regex / variable interpolation should be added to regexes in languages probably :)
> Also regex / variable interpolation should be added to regexes in languages probably
I've often wondered and desired this myself. It feels like the single most useful change one might make, because it enables (albeit very basic) logical decomposition. It comes in handy when a regex works for your use case, but the regex is somewhat long but with many repeated parts. In my experience, that isn't too rare, and the regex readability would likely be helped quite a bit by some kind of decomposition.
let day = "[0-9]{2}"
let month = "jan|feb|…|dec"
let year = "[0-9]{4}"
let regex = Regex(month + " " + day + ", " + year)
Disadvantage is that, if your programming language has automatic syntax check of regular expressions, you probably lose that.The string concatenations also make things a bit slower, but that almost always needs to be done only once.
Advantage is that this makes testing easier. For example, you can do
let dayRegex = Regex(day)
and then test that regex in isolation. That probably is overkill for this example, but if you change the regex to only accept numbers in [1,31], that can be useful.If you decide to have your regex to treat 30-day and 31 day months separately, things get more complicated. I wouldn’t go that far, though, and certainly not as far as handling leap years correctly. When regexes get complex, it’s time to go for a proper lexer/parser.
Still doesn't change what I said.
My original comment was talking about adding variables to the regex language itself. String interpolation and combinator APIs and enums and type safety are all beside the point. They surely solve a similar problem as regex variables in certain contexts, but they go about it in a very different way and are specific to the context in which the regex is created.
https://github.com/search?l=Rust&p=5&q=%22regex%3A%3Anew%22&...
For the importance of numeric range it was quite easy to find actually a good example:
x0rz3q/advent-of-code-2020
Regex::new(r"^19[2-8][0-9]|199[0-9]|200[0-2]$").unwrap(), ); comparitor.insert("iyr", Regex::new(r"^(201[0-9]|2020)$").unwrap());
There are lots of number parsing.
I would enable both [[:1-12:]] and [[:01-12:]] as options without / with leading zeros.
About the variables:
This file would look much more readable with variables that are reusing other regexes:
https://github.com/spcan/common-regex/blob/3238bc8ee85e0e000...