Matching any character except an underscore using Regex
Asked Answered
P

2

10

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

Photon answered 4/3, 2013 at 16:10 Comment(1)
^[^_]+$ this means any string without an underscore. e.g., this comment or your whole question. Is that what you want?Shylashylock
W
9

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)$

Witchcraft answered 4/3, 2013 at 16:45 Comment(3)
How does this ensure no underscore after the @?Photon
Because the character group [A-Za-z0-9] does not contain the underscore character, and consequently underscores won't match. The only valid strings after the @ sign are one or more characters, followed by a period, followed by one of (com|ca|org|net).Witchcraft
If you aren't already, I recommend a site such as Regex Pal to test your regular expressions against test strings.Witchcraft
J
17

/^[^_]+$/ would match a string of 1 or more character containing any character except underscore.

Jumna answered 4/3, 2013 at 16:12 Comment(0)
W
9

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)$

Witchcraft answered 4/3, 2013 at 16:45 Comment(3)
How does this ensure no underscore after the @?Photon
Because the character group [A-Za-z0-9] does not contain the underscore character, and consequently underscores won't match. The only valid strings after the @ sign are one or more characters, followed by a period, followed by one of (com|ca|org|net).Witchcraft
If you aren't already, I recommend a site such as Regex Pal to test your regular expressions against test strings.Witchcraft

© 2022 - 2024 — McMap. All rights reserved.