16 ms·
Author of Rust's regex crate here. > Also regex / variable interpolation should be added to regexes in languages probably I've often wondered and desired this
by burntsushi 4y ago
Author of Rust's regex crate here.
> 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.
- Someone 4y agoYou can do that by building up a string and converting that to a regex. Pseudo-code: 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.
- burntsushi 4y agoOf course I'm aware of that technique and I've used it many times. :-) Doesn't change what I said.
- ogogmad 4y agoSurely, you can use Rust's enums for this.
- burntsushi 4y agoAs I said above, I'm the author of Rust's regex crate. I know what Rust enums are. Still doesn't change what I said.
- ogogmad 4y agoI mean that it's typesafe.
- burntsushi 4y agoI still don't know what you're talking about. I didn't say anything about type safety? Nobody else did? I guess you're probably talking about a combinator API... Like this one: https://docs.rs/regex-syntax/latest/regex_syntax/hir/struct.Hir.html https://docs.rs/regex-syntax/latest/regex_syntax/hir/struct.... 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.
- xiphias2 4y agoIt's easy to see the usages on github: https://github.com/search?l=Rust&p=5&q=%22regex%3A%3Anew%22&type=Code 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/3238bc8ee85e0e000ea20913f09db6477edb2922/src/common.rs https://github.com/spcan/common-regex/blob/3238bc8ee85e0e000...
- rjh29 4y agoPerl's been doing this for ages with the qx/.../ operator. It indeed works great for logical composition although Raku's first-class grammars are a much better evolutionary step IMO.