Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add custom User Tokens for regular expressions? #306

Open
XRay3D opened this issue May 22, 2024 · 1 comment
Open

Is it possible to add custom User Tokens for regular expressions? #306

XRay3D opened this issue May 22, 2024 · 1 comment

Comments

@XRay3D
Copy link

XRay3D commented May 22, 2024

Is it possible to add custom UserTokens for regular expressions?
For example: \F
For float numbers:

+.123
-1.5e-10
3.14

Or you can give an example, or where to look, how to do this.

@OlivierReubens
Copy link

CTRE is C++ code, so "yes", but not as part of the actual regex grammar...

#define RX_FLOAT "whatever you want here"

auto res = ctre::match<RX_FLOAT " plus " RX_FLOAT " equals " RX_FLOAT>(teststring);

Don't like defines? you can also get it done by using a compile literal time string ( a bit more advanced than the one in CTRE so you can use + operators).

And you can also split up the regex in multiple ctre calls which may result in better code because the compiler only needs to generate the float pattern match once, which depending on the regex it may or may not do on it's own. This may even end up with more readable C++ code and not require the C++ dev to also be fluent in regex, which may be an issue with really complex regexes. Also... repeating a complex sub-pattern multiple times into a larger pattern may cause quite slow compilation because of multiple rounds in the optimizer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants