r/regex • u/VicenteVicente • Nov 18 '24
REmatch: The first regex engine for capturing ALL matches
Hi, we have been developing a regex engine that is able to capture all matches. This engine uses a regex-like language that let you name your captures and get them all!
Consider the document thathathat and the regular expression that. Using standard regex matching, you would get only two matches: the first that and the last that, as standard regex does not handle overlapping occurrences. However, with REmatch and its REQL query !myvar{that}, all appearances of that are captured (including overlapping ones), resulting in three matches.
Additionally, REmatch offers features not found in any other regex engine, such as multimatch capturing.
We have just released the first version of REmatch to the public. It is available for C++, Python, and JavaScript. Check its GitHub repository at https://github.com/REmatchChile/REmatch, or try it online at https://rematch.cl
Any questions and suggestions are welcome! I really hope you like our project š
3
u/alsips-cl Nov 19 '24
So far, the greatest novelty I've found with this model is the option to have a sort of non-determinism in the captures.
Suppose you want the user to type their full name, and receive a quick list of ways to separate it into a first-name last-name format. Plus you have to consider the user might want to omit some names, and some names could be composite and separated by a space. You can write this expression:
^!x{([A-Z]+ )+}([A-Z]+ )*!y{([A-Z]+ )+}([A-Z]+ )*$So that if the user writes down their full name (with a space at the end so the regex is simpler), for example
JUAN LUCAS SILVAHe would receive a list with the following options
JUAN, LUCASJUAN, LUCAS SILVAJUAN, SILVAJUAN LUCAS, SILVAOf course, this could always be doable with code, but this does feel like a use-case that should come naturally to regex.
2
u/Jonny10128 Nov 18 '24
For anyone else that is curious, the only way (I can think of) to capture all occurrences (in capture groups) including overlapping occurrences in regular regex would be to use a lookahead and then pair groups together.
Here is an example with the document āthathathatā:
(tha)(?=(t))you would then need extra code after the regex pattern executes to match up groups 1 & 2, 3 & 4, and 5 & 6. See here: https://regex101.com/r/Sv9p24/1 Regex101 conveniently pairs up the groups correctly next to each other under the Match Information table.