topherg
topherg

Reputation: 4303

Suitable regular expression for replacing elements in a string

I am building a router library for converting a simply defined pattern into a regular expression that can later be used for comparing a url. This is so rules can be defined simply, and the library will take care of the rest.

Lets assume the string /*/#:id/# is passed into the library, I would want to get /^\/(.{1}.*)\/(?<id>[0-9]{1}[0-9]*)\/([0-9]{1}[0-9]*)$/i. Initially, the library was designed to handle just converting * and # into match any text ((.{1}.*)), and match a number (([0-9]{1}[0-9]+)) respectively, but now I want to include the option to name specific matches. Doing a simple string replace would not work.

Initially I was thinking about having a function parse through the entire string, when a the characters * or # is encountered, it will check the next character, and if it is a :, it will continue until a non a-z character is encountered, at which point it will take that phrase, enclose it within ?< and >, then the suitable pattern for what was previously found, and finally enclose the whole thing within brackets, then continue on, but in practice, this is not the most efficient way, and when I try to define upwards of 50 rules, the processing time would take approximately 50ms, and when you consider that this is still part of the bootstrap process, it seems too long, especially when a complex webapp might have upwards of 200 url rules.

I have had a look at regular expression replace functions, but for the moment, it is going a bit over my head... I am not sure if this would be the best solution, if a solution at all. So, does anyone know how it could be achieved, or perhaps have a better solution that could perform what I am trying in a timely fashion?

Additional

I should also note, that some rules I include might look like:

/*:init-test/#/*

which would match url's like

/foo-test/123/asdf
/bar-test/456/jkl

Another rule I am planning might look like this:

/*:init:test/#/*

which would match url's like

/footest/123/asdf
/bartest/456/jkl

Upvotes: 1

Views: 88

Answers (1)

Guvante
Guvante

Reputation: 19223

I would say regular expression your regular expression. First you need to escape anything that is special, should be easy enough (and the syntax is annoying) so I will skip over that bit.

Next you will want to replace your special cases, first any named things.

\*(:[A-Za-z]+:?)? ---> (?<\2>.+)
\#(:[A-Za-z]+:?)? ---> (?<\2>\d+)

And then the non-named things

\* ----> .+
\# ----> \d+

Upvotes: 1

Related Questions