I'm really bad with regex. For the purposes of URI rewriting, I'm trying to figure out how to match a word one or more times in a string.

For example, I want to do something like:

string/string/string/foo/bar => foo/bar
string/string/bazbat => bazbat

Currently I have:

^string/(.*)$ => $1

and that works, but I want to make string/ work for one or more times, not just once.

This didn't work either:

^((\bstring\/\b)+)(.*)$ => $2

Can you explain one more time in more detail? I couldn't make any sense out of your explanation.

Codeigniter, the PHP framework that I use, has a URI routing mechanism that, for all intents and purposes, functions similar to Apache's mod_rewrite.

I am having some difficulty in building a rewrite rule that filters out a word that appears one or more times at the beginning of a URI, such that it would translate the following:

string/string/string/foo/bar into foo/bar
string/string/bazbat into bazbat
etc.

I had ^string/(.*)$ => $1 and that, of course worked when string/ only appears exactly once, but I want to account for it appearing one or more times.

Currently I have ^((\bstring\/\b)+)(.*)$ => $2 and it doesn't work.

You don't need a regular expression for that. Just replace all occurrences of "string/" with "".

Correct. That's effectively what I'm trying to do, but through the URI routing mechanism, which of course doesn't have access to native PHP functions.

Also ... I don't want to remove all occurrences. Just one or more repeated occurrences at the beginning of the string.

Oh!! Never mind! You gave me the answer after all :)

It turns out that Codeigniter's URI routing supports callback functions and I never knew!!

P.S. Yes, we're still on CI3, although I'm not so sure I should be announcing that as it's reached end of life and doesn't receive maintenance/security updates anymore.

Follow up: I marked this thread as solved, because it did indeed solve my problem at hand, but I then unmarked it as solved because I'm hoping someone out there will still be able to come up with the correct regex to help anyone not lucky enough to be using a URI router that supports callbacks, and mod_rewrite is of course still incredibly popular.

Still working on it, but to match one or more occurrences of string/ starting at the beginning of the string you would specify

^(string/){1,}

then if you replaced the matched expression with "" you would end up with what you want.

commented: Thanks! That worked :) +34

That worked! :)

^(string/){1,}(.*)$ => $2

It actually worked for:

string/string/string/foo/bar => foo/bar
string/string/bazbat => bazbat

but to additionally match on:

string/string

I made the following change:

^(string/?){1,}(.*)$ => $2

Marking as solved for realsies this time! Thanks!!

There is an old comp sci saying... "I had a problem and was told it could be solved with a regular expression. Now I have two problems."

Glad to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.