Page 1 of 1

Lookbehind and Start of Line Anchor

Posted: Sat Sep 19, 2009 11:51 am
by veleshanas
Hello forum,

I am having trouble using start of line anchor together with lookbehind assertion. I appreciate your help!

What I would like to do is to define a preg_replace formulation that converts "ag ita" into "acca" or "ag ite" into "acce". The first string consists of two words and the conversion makes it into a single word. The important thing is not to have this conversion take place if "a" is preceded by other character(s).

I thought it is very easy using line anchor together with lookbehind assertion but strangely my following regex doesn't work. What could be wrong here? :banghead:

preg_replace('/g(?<=^[a]) it/', 'cc', $source);

Goal:
ag ita ---> acca
but not
pag ita ---> pacca

Re: Lookbehind and Start of Line Anchor

Posted: Sat Sep 19, 2009 1:27 pm
by prometheuzz
This part of your regex: 'g(?<=^[a])' makes no sense. It means: "match a 'g' and the empty string after that 'g' should have the start of the string and an 'a' before it". Needless to say, that can never happen.
Try:

Code: Select all

preg_replace('/^ag it[ae]/', 'acc', $source);

Re: Lookbehind and Start of Line Anchor

Posted: Sat Sep 19, 2009 6:18 pm
by veleshanas
Try:

Code: Select all

preg_replace('/^ag it[ae]/', 'acc', $source);
Thanks. I wrote "'/^ag it/', 'acc'" and it worked fine. The second word may not be limited to ita and ite, so I chopped off the [ae] part.
prometheuzz wrote:This part of your regex: 'g(?<=^[a])' makes no sense. It means: "match a 'g' and the empty string after that 'g' should have the start of the string and an 'a' before it". Needless to say, that can never happen.
Currently, the above is enough but I may have to scale the regex. My first attempt was to use a more general definition. That is, "match a 'g' when it follows the start of the string and a single character in the class of X, where X is given in the list [...]".

I think I've found what I was looking for:

Code: Select all

preg_replace('/(?<=^[aeiou])g it/', 'cc', $source);