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
[^ ] matches anything but a space character.
[0-9] —The string can contain any number between 0–9. [_-] —The string can contain an underscore or hyphen. Both the underscore and the hyphen are called special characters. Special characters include any non-alphanumeric characters, such as punctuation or symbols.
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.
/^[^_]+$/
would match a string of 1 or more character containing any character except underscore.
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)$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With