473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

regex negative look behind not working

Hi ... want to antispam any email address that is written in a page, so long
as it is not already contained in a hyperlink ie 'mailto:us**@email.com'

so am trying to use a negative look behind but I can't get it to work

preg_replace('/(?<!mailto)(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',
"<script>document.write('<a
href=\'mailto:'+'\\1'+'@'+'\\2'+'\'>'+'\\1'+'@'+'\ \2'+'</a>');</script>",$st
ring );

doesn't ignore strings starting with mailto (have also tried using
'mailto\\:'
Sep 13 '05 #1
2 2355
That doesn't work, because the pattern will match again as soon as the
regexp engine looks one letter to the right.

The bracketed letter below indicates the first letter of a possible
match. The following fails the lookbehind assertion, because "mailto:"
matches the condition:

mailto:[c]he**********@hotmail.com

The following does not fail the assertion, as "mailto:c" is not a
match:

mailto:c[h]er*********@hotmail.com
A job like this is easier with preg_replace_callback(), arguably the
most powerful text manipulation function in PHP. Example:

function antispam($m) {
return ($m[1]) ? $m[0] :
"<script>document.write('<a
href=\'mailto:'+'{$m[3]}'+'@'+'{$m[4]}'+'\'>'+'$m[3]'+'@'+'$m[4]'+'</a>');</script>";
}

preg_replace_callback('/(mailto:)?\s*(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',
'antispam', $string);

Sep 13 '05 #2

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
That doesn't work, because the pattern will match again as soon as the
regexp engine looks one letter to the right. A job like this is easier with preg_replace_callback(), arguably the
most powerful text manipulation function in PHP. Example:

function antispam($m) {
return ($m[1]) ? $m[0] :
"<script>document.write('<a
href=\'mailto:'+'{$m[3]}'+'@'+'{$m[4]}'+'\'>'+'$m[3]'+'@'+'$m[4]'+'</a>');</
script>"; }

preg_replace_callback('/(mailto:)?\s*(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i', 'antispam', $string);

Excellent, this works perfectly.
Sep 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bhargava | last post by:
Hello, I am a newbie to python and need some help. I am looking at doing some batch search/replace for some of my source code. Criteria is to find all literal strings and wrap them up with...
2
by: Stevie | last post by:
Hello I'm trying to work out the regular expression for carrying out a simple 'greater than' comparison. I have a directory with files such as asd345.log, asd346.log, asd347.log and so on.
5
by: skavan | last post by:
Hi, I'm just wrapping my head around regex and am pretty sure it can do the task at hand - but it's too complex for my brain to process -- so am throwing it out there for you experts to comment...
2
by: writebrent | last post by:
I think I need to do a negative lookahead with a regular expression, but I'm a bit confused how to make it all work. Take these example texts: Need to match these two: =========================...
5
by: MrNobody | last post by:
is there a simple way to make it so your regex only matches whole words? i was thinking simply something like: *match_string* but then I think it would fail if the word was at the beginning...
6
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
2
by: Jeremy | last post by:
I am trying to replace a string "P" with "\P" as long as the string "P" does not already have a "\" in front. for my search string I've used @"(P)" so my regex replace statement is: ...
3
by: Smokey Grindel | last post by:
Alright so I have a string... that can be anything like this then have a number like 102.34m, yes there is a m behind it to say "this is money", no I didn't design the spec thats just how data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.