Quick Regex question
This is a php Regex, but I have a feeling I can find help here...
I am usingthis code to convert a email@email.com to <a href="mailto: ..">
I want to modify it soif there's already a <a href="mailto:...> it will not change it
My current code is:
$text = eregi_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","><a href=\"mailto:\\1\">\\1</a>", $text);
Help please.
www.MathLessons.com - When the numbers don't add up.
[654 byte] By [
tomerg3a] at [2007-11-27 10:57:56]

This doesn't look like Java to me, more like Perl.
But since regex isn't actually language-specific (apart from some minor differences):
You might want to look at a "zero-width negative lookahead" or something of that kind:
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
I think it would look something like this:
"(?!<a href=\"mailto:[^\"]*\">)([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"
> This is a php Regex, but I have a feeling I can find
> help here...
>
> I am using this code to convert a
> email@email.com to <a href="mailto: ..">
>
> I want to modify it so if there's already a <a
> href="mailto:...> it will not change it
>
> My current code is:
> $text =
> eregi_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[
> a-z]{2,3})","<a href=\"mailto:\\1\">\\1</a>",
> $text);
>
> Help please.
>
>
>
> www.MathLessons.com - When the numbers don't add up.
Dude... why the hell are you using regex here? There's no point.
if (!strstr($text, 'href="mailto:"')) {
$temp = $text;
$text = '<a href="mailto:"'.$temp.'">';
}
> Dude... why the hell are you using regex here?
> There's no point.
>
> > if (!strstr($text, 'href="mailto:"')) {
>$temp = $text;
> $text = '<a href="mailto:"'.$temp.'">';
> }
>
Dude... Where's my car?!?!?!
$text has other text in it, not just the email, it can be something like :
"You can email me at my@email.com and I can also have <a href="mailto:my@email.com>Email me</a>
and I don't want to mess the existing <a href>">
I tried doing
"([^mailto:])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"
but it doesn't work.
(?<!mailto:)([_\.0-9a-z-]+@[0-9a-z][0-9a-z-]+\.+[a-z]{2,3})
>
> (?<!mailto:)([_\.0-9a-z-]+@[0-9a-z][0-9a-z-]+\.+
> [a-z]{2,3})
that doesn't work :(
I'm going to try my luck on a php forum, thanks anyways
use pregi_replace instead of eregi with that one.
> ...
> that doesn't work :(
>
> I'm going to try my luck on a php forum, thanks
> anyways
Not a bad idea since it's PHP you're asking about.
Here's how I'd do it in Perl:
$text = "You can email me at my\@email.com and I can also have <a href=\"mailto:my\@email.com>Email me</a>";
print "$text\n";
$text =~ s/(?<=\s)(\w+\@\w+[.]\w+)(?=\s)/<a href=\"mailto:$1>Email me<\/a>/;
print "$text";
# Output:
#You can email me at my@email.com and I can also have <a href="mailto:my@email.com>Email me</a>
#You can email me at <a href="mailto:my@email.com>Email me</a> and I can also have <a href="mailto:my@email.com>Email me</a>
> use pregi_replace instead of eregi with that one.
that didn't work either