www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ctRegex with variable?

reply Jethro <qyzz gr.ff> writes:
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.
Apr 12 2017
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 12 2017
prev sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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.
Apr 13 2017