With an added quantifier that repeats the character class one or more times, and anchors that bind the match to the start and end of the string, we’re good to go. A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. A character class can set up the allowed range of characters. Matches any number of characters including special characters. Answer originally given on this Stack Overflow link. If we want to match a set of characters at any place then we need to use a wild card character ‘ ‘ (asterisk) which matches 0 or more characters. For example if I wanted to match a number made up of one or more digits if there is a number, but still want to indicate a match if the rest of the expression fits, I could specify 0-9 + Match one or more of the characters in the preceding expression. To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a and includes every character until the end by finishing with a. With alphanumeric regex at our disposal, the solution is dead simple. As requested in the comments below, some regex to remove trailing whitespaces from lines with text and empty lines. Match zero or more of the characters in preceding expression. Alphanumeric characters are all alphabets and numbers i.e. We can use the given regular expression used to validate user input in such a way that it allows only alphanumeric characters.