Lookbehind and Start of Line Anchor

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
veleshanas
Forum Newbie
Posts: 10
Joined: Sat May 24, 2008 10:58 pm

Lookbehind and Start of Line Anchor

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Lookbehind and Start of Line Anchor

Post 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);
veleshanas
Forum Newbie
Posts: 10
Joined: Sat May 24, 2008 10:58 pm

Re: Lookbehind and Start of Line Anchor

Post 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);
Post Reply