1

Where I'm running into a roadblock is trying to check for this

(?<http>(http|ftp|https):\/\/)?(?<address>([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)

while rejecting the expression if it contains the @ sign.

I've tried a number of variations on this with no success.

What would work is if I can tell it to make sure that there are no @ characters in it but I don't want to require a character using the [^@] syntax. So how do I tell regex to accept \w and reject @ at the same time. I can't do [\w^@] to the best of my knowledge.

My Difficulty is that I'm using regex replace. Ar you suggesting then that I use a placeholder and ten match against that afterwards?

Jon B
  • 51,025
  • 31
  • 133
  • 161
Middletone
  • 4,190
  • 12
  • 53
  • 74
  • Some examples of valid and invalid text would be helpful – Jaimal Chohan Sep 12 '09 at 00:28
  • www.cnn.com is a valid address jim@cnn.com is not and should not be altered. – Middletone Sep 12 '09 at 00:29
  • Even if you don't want to allow username@ URLs, http://www.example.com/foo?x=y@z is still a valid address. Validating either an e-mail address or a URL is very vrey hard to do properly in regex; allowing a bare hostname to work as a URL too makes it even less possible. – bobince Sep 12 '09 at 02:28
  • I just need to make sure that when I do regex.replace that hte replacement doesn't include the @ symbol. That's my only hangup here. I've tried negative lookbehind and that doens't seem work either. – Middletone Sep 12 '09 at 02:35
  • It has to be on one line all together. The problem occurs when it starts to match the domain in the email and thinks that it's a web address. – Middletone Sep 12 '09 at 02:39

2 Answers2

3

You don't have to get everything into one regex. You could for example, match against your existing regex, then check with a secondary regex that the string doesn't have an @-sign.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Yep, does not need to be one regex for one task. Mulitple regexs are maybe clearer. Maybe this link can give some more hints: http://www.regular-expressions.info/email.html – Theo Lenndorff Sep 12 '09 at 16:27
0

I agree with Ned. Use two regular expressions. You can get them from several sources, including http://regexlib.com/

TrueWill
  • 25,132
  • 10
  • 101
  • 150