473,804 Members | 4,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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**@em ail.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>docume nt.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 2378
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**********@ho tmail.com

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

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

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

preg_replace_ca llback('/(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.goo glegroups.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_ca llback(), arguably the
most powerful text manipulation function in PHP. Example:

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

preg_replace_ca llback('/(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
2063
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 some macro, say MC. For ex., var = "somestring" would become var = MC("somestring"). Literal strings can contain escaped " & \. But there are 2 cases when this replace should not happen:
2
2577
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
1724
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 on. I am posing two questions. In the interests of space and focus, I'll post a separate thread for the other use case (clustering). Use Case 1: Filenames contain a TrackNumber (or not).
2
7267
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: ========================= Item 4.01 Regulation and other items <b>Item 4. Regulation</b>
5
10578
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 or end of a line?
6
9669
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 words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
4
312
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 URLs. (href=("|')http://.www\.thesite\.com/.{1,7}/)(?<filename>.) This generally works, but the problem is some of the image files have ..th.jpg at the end to indicate thumbnails. I want to exclude those. I just want the ones that don't have...
2
1527
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: System.Text.RegularExpressions.Regex pR = new System.Text.RegularExpressions.Regex(@"(P)"); string strResult = pR.Replace(@"This is a XP test X\P", @"\P"); The poblem is that the statement replaces XP with \P where I want it to be
3
2533
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 comes in... well I had a regex that replaces all those m's in a string where there is a number before it with no m... so its just the string with 102.34... well this worked fine until we got negative money! so now when its -102.34m it still has the...
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.