I am having problems creating a regex validator that checks to make sure the input has uppercase or lowercase alphabetical characters, spaces, periods, underscores, and dashes only. Couldn't find this example online via searches. For example:
These are ok:
Dr. Marshall sam smith .george con-stanza .great peter. josh_stinson smith _.gorne
Anything containing other characters is not okay. That is numbers, or any other symbols.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter.
A regex (regular expression) consists of a sequence of sub-expressions. In this example, [0-9] and + . The [...] , known as character class (or bracket list), encloses a list of characters. It matches any SINGLE character in the list.
Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.
The regex you're looking for is ^[A-Za-z.\s_-]+$
^
asserts that the regular expression must match at the beginning of the subject[]
is a character class - any character that matches inside this expression is allowedA-Z
allows a range of uppercase charactersa-z
allows a range of lowercase characters.
matches a period rather than a range of characters\s
matches whitespace (spaces and tabs)_
matches an underscore-
matches a dash (hyphen); we have it as the last character in the character class so it doesn't get interpreted as being part of a character range. We could also escape it (\-
) instead and put it anywhere in the character class, but that's less clear+
asserts that the preceding expression (in our case, the character class) must match one or more times $
Finally, this asserts that we're now at the end of the subjectWhen you're testing regular expressions, you'll likely find a tool like regexpal helpful. This allows you to see your regular expression match (or fail to match) your sample data in real time as you write it.
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