Hacker News new | ask | show | jobs
by volta83 1889 days ago
> use a regex to find a string in some text, escape all the regex-reserved characters in that string, then use the string as a regex to find other occurrences of itself in the text)

This took 2 min: https://play.rust-lang.org/?version=stable&mode=debug&editio...

and I don't recall what's the last time I used the regex crate.

I'm certainly doing all the regex stuff incorrectly, no idea how to match regex-reserved characters, no idea if I'm using the iterator API correctly (its the second example in the regex docs..), etc.

But the idea is simple, search the text for the regex, escape all the reserved characters, use that regex to search for occurrences of the same regex.

I guess that if you want a fully-vectorized, zero-allocation, zero-copy, multi-threaded, gpu... version of this that maxes out the theoretical peak of the hardware available, then you'll probably end up putting a considerable amount of work here.

But otherwise, for someone with literally 30 seconds of experience with the regex crate, it was as straightforward to implement as you mentioned. Just c&p their second example, and just added some glue. This is how I'd implement this in Python, or Java...

EDIT: other responses suggest cloning, but that seems to suggest that one has to explicitly and deliberately think about cloning. I instead just played a game of type golf using functions like `.to_string()` or `.collect` to "clone" stuff behind the scenes. My code is probably horrible, but is the first thing that came to mind.

1 comments

Also, this function is just regex::escape https://docs.rs/regex/1.4.5/regex/fn.escape.html