Click to See Complete Forum and Search --> : preg_replace help plz...


rch10007
04-08-2006, 01:31 AM
I have a file that uses @ as a delimiter. I am trying to change the string before the first delimiter only.

The lines in the file look like this:

sometext123@moretext456@moretext
sometext123@moretext456@moretext
sometext123@moretext456@moretext
....

What I am trying to do is eliminate the letters before the first @, so the resulting lines look like:

123@moretext456@moretext
123@moretext456@moretext
123@moretext456@moretext
....

this is what I have tried using and it doesn't work!


foreach($text as $line)
{
fwrite($fh, preg_replace('/^[A-Za-z]@$/', '', $line));
}

note* assume i have the file open adn its readable.

also, why is what I have not working - i really am trying to get PCRE but it is so confusing!!

^ = start searching at the beggining right?
[A-Za-z]@ = thsi is the pattern i am looking for
$ = stop searching this line??
'' = replace it with nothing

is this not correct (i guess so, but how)?

thanks

felgall
04-08-2006, 03:04 AM
$ = end of string and is the character that is stopping your replace from working since in every case the @ is not the last character in the string. Also you are not allowing for anything between the letters and the @ so with numbers in there it wont match either.

Try this instead which will simply remove any letters on the front of the line regardless of what else it contains after that:

fwrite($fh, preg_replace('/^[A-Za-z]+/', '', $line));

rch10007
04-08-2006, 03:12 AM
Thanks, that worked - what is telling it to stop searching? Isn't + supposed to mean more than 1 so keep going?

NogDog
04-08-2006, 06:54 AM
+ = 1 or more of the preceding character or group
[A-Za-z] = any alpha character
^ at the start of the regexp means the start of the string being searched

So the search is matching on one or more alpha characters where the first character is at the start of the string. The match ends just before the first non-alpha character encountered after the first one.

rch10007
04-08-2006, 06:59 AM
ok! now it makes sense - charles, what tutorials do you suggest for learning more about this?

I have searched online for some and found one that seems to be aimed at those less familiar with the PERL language.

Just wondering if you know off hand of any, please don't go searching for any. thx!

NogDog
04-08-2006, 07:07 AM
Off-hand I don't have any tutorials to recommend. I started learning regexp's several years ago while creating some Perl scripts, using the O'Reilly Programming Perl book (the "camel" book").

Mostly I just use this page now: http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php

rch10007
04-08-2006, 07:18 AM
k, thx!