Thread overview | |||||
---|---|---|---|---|---|
|
April 12, 2017 ctRegex with variable? | ||||
---|---|---|---|---|
| ||||
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 Re: ctRegex with variable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jethro | 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 Re: ctRegex with variable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jethro | 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.
|
Copyright © 1999-2021 by the D Language Foundation