Thread overview
ctRegex with variable?
Apr 12, 2017
Jethro
Apr 12, 2017
Ali Çehreli
Apr 13, 2017
Jesse Phillips
April 12, 2017
Can regex's have variables in them? I'd like to create a ctRegex but match on runtime strings that are known at runtime.

e.g.,

auto c = ctRegex~("x{var}")

where var is a variable that is passed at runtime. e.g., match(s, c, "test") will replace var with test.

The reason is I basically have the same regex to match but each one differs in a specific way. It would be nice to be able to use a fast way to search since most of the regex is the same but only the a single token differs(and these tokens are only known at runtime).

I obviously can build the regex string at runtime like

auto var = "test"
auto c = Regex("x{"~var~"}");

or whatever... but this is much slower when only var changes.

April 12, 2017
On 04/12/2017 02:25 PM, Jethro wrote:
> Can regex's have variables in them? I'd like to create a ctRegex but
> match on runtime strings that are known at runtime.
>
> e.g.,
>
> auto c = ctRegex~("x{var}")
>
> where var is a variable that is passed at runtime. e.g., match(s, c,
> "test") will replace var with test.

Sounds reasonable but the string would have to be constructed each time match is called. It could reuse the same buffer... So, one needs to profile to see how it performs and we expect you to implement and test it please. ;)

Ali

April 13, 2017
On Wednesday, 12 April 2017 at 21:25:40 UTC, Jethro wrote:
> Can regex's have variables in them? I'd like to create a ctRegex but match on runtime strings that are known at runtime.
>
> e.g.,
>
> auto c = ctRegex~("x{var}")

As mentioned by Ali, benchmark for your use case.

If var has common values (e.g. 1-1000). generate a ctRegex table for those values and use runtime for any that aren't in the table.