I find that I have a regex that I want to use on its own as well as part of another, bigger regex. But I didn't find a way to compose the bigger one out of multiple smaller ones neatly.
The concrete problem is that I'm annotating Dockerfiles with a pattern to find updates, like this:
# uptag --pattern "<!>.<>"
FROM ubuntu:14.03
I need to find the pattern and the following FROM <image>
definition, for which I'm currently using regex. But the image after FROM
can not only appear in a Dockerfile, but also in a docker-compose file as a service's image
field. So I'd like to define a single regex for <image>
and reuse it in both places.
How can I build the bigger regex, that finds the pattern plus the FROM <image>
, while reusing a smaller regex for <image>
?
I have the feeling that the regex
crate does not cover this case. So I am thinking about using a parser cobinator library like combine
(similar to nom
) to find this pattern and extract the relevant information, so that I can reuse the component parsers. Are there disadvantages to such a parser when compared to regex
? For example, does regex
do something that is significantly faster than a parser walking over the input char by char, or should the performance be comparable?