Matching any character except an underscore using Regex

regex, regex-negation

Solution

If I understand what you're asking for - matching strings of characters, except for strings of characters that contain an underscore - this requires regex lookahead.

The reason is that regular expressions normally operate one character at a time. So if I want to know if I should match a character, but only if there is not an underscore later, I need to use lookahead.

`^((?!_)[A-Za-z0-9])+$`

`?!` is the negative lookahead operator

EDIT:

So you want there to be at most one underscore in the portion before the @ sign, and no underscore in the portion after?

`^[A-Za-z0-9]+_?[A-Za-z0-9]+@[A-Za-z0-9]+\.(com|ca|org|net)$`

Problem

I'm trying to write Regex that will match a string of characters EXCEPT strings with an underscore. I have this so far `/[A-Za-z0-9]+/` but I don't know what to include in it to make it require no underscore. UPDATE: Is should have made this more clear off the bat. I am trying to match an email address, but not email addresses that have an underscore in the portion after the _ This is what I have in total so far. `/[A-Za-z_0-9]+@[A-Za-z0-9]+\.(com|ca|org|net)/` The answers as of yet, don't work

Original source