-
Notifications
You must be signed in to change notification settings - Fork 1
classes
List characters inside of [] to match one of any listed
| Syntax | Meaning |
|---|---|
/[regex]/ |
Match one character that was in the word regex. In this case, the second e is ignored. |
/[a-z]/ |
Match any lower case letter |
/[a-zA-Z] |
Any letter |
/[a-z.]/ |
Lower case letters and a literal period. |
/[-abc]/ |
Exception: - is first, so it is the actual character, not a range |
/[abc-]/ |
Exception: - is last, so it is the actual character, not a range |
/[^aeiou]/ |
Exclude vowels. A ^ as the first character changes the mode. |
/[^a-z]/ |
Not a lower case |
A special escape sequence can be used to refer to a predefined class of character instead of having to create your own set. For example, /[0-9]/ is the same as /\d/.
| Class | Meaning |
|---|---|
| \w | Match a "word" character (alphanumeric plus "_", plus other connector punctuation chars plus Unicode marks) |
| \W | Match a non-"word" character |
| \s | Match a whitespace character |
| \S | Match a non-whitespace character |
| \d | Match a decimal digit character |
| \D | Match a non-digit character |
| \pP | Match P, named property. Use \p{Prop} for longer names |
| \PP | Match non-P |
| \X | Match Unicode "eXtended grapheme cluster" |
| \N | Any character but \n. |
| \v | Vertical whitespace |
| \V | Not vertical whitespace |
| \h | Horizontal whitespace |
| \H | Not horizontal whitespace |
| \R | Linebreak |
Posix classes go inside a normal alternation. Some languages may have an alterate syntax.
-
/[[:word:]]/will match a normal letter with standard syntax -
\p{Word}is the alternate syntax
Copied from Cheat sheet for character classes
| Class | Meaning |
|---|---|
| [:alpha:] | Any letter, [A-Za-z] |
| [:upper:] | Any uppercase letter, [A-Z] |
| [:lower:] | Any lowercase letter, [a-z] |
| [:digit:] | Any digit, [0-9] |
| [:alnum:] | Any alphanumeric character, [A-Za-z0-9] |
| [:xdigit:] | Any hexadecimal digit, [0-9A-Fa-f] |
| [:space:] | A tab, new line, vertical tab, form feed, carriage return, or space |
| [:blank:] | A space or a tab. |
| [:print:] | Any printable character |
| [:punct:] | Any punctuation character: ! ' # S % & ' ( ) * + , - . / : ; < = > ? @ [ / ] ^ _ { |
| [:graph:] | Any character defined as a printable character except those defined as part of the space character class |
| [:word:] | Continuous string of alphanumeric characters and underscores. |
| [:ascii:] | ASCII characters, in the range: 0-127 |
| [:cntrl:] | Any character not part of the character classes: [:upper:], [:lower:], [:alpha:], [:digit:],[:punct:], [:graph:], [:print:], [:xdigit:] |
- Find three letter acronyms (Three upper case letters in a row). Ex: ACE, NPE, HCF, etc.
- Find four letter words
- Find the letters in your name
- Find the letters that are not in your name
The IEEE POSIX standard has three sets of compliance: BRE, ERE, and SRE for Basic, Extended, and Simple Regular Expressions. SRE is deprecated, in favor of BRE, as both provide backward compatibility. The subsection below covering the character classes applies to both BRE and ERE.
BRE and ERE work together. ERE adds ?, +, and |, and it removes the need to escape the metacharacters ( ) and { }, which are required in BRE. Furthermore, as long as the POSIX standard syntax for regexes is adhered to, there can be, and often is, additional syntax to serve specific (yet POSIX compliant) applications. Although POSIX.2 leaves some implementation specifics undefined, BRE and ERE provide a "standard" which has since been adopted as the default syntax of many tools, where the choice of BRE or ERE modes is usually a supported option. For example, GNU grep has the following options: "grep -E" for ERE, and "grep -G" for BRE (the default), and "grep -P" for Perl regexes.
Perl regexes have become a de facto standard, having a rich and powerful set of atomic expressions. Perl has no "basic" or "extended" levels, where the ( ) and { } may or may not have literal meanings. They are always metacharacters, as they are in "extended" mode for POSIX. To get their literal meaning, you escape them. Other metacharacters are known to be literal or symbolic based on context alone. Perl offers much more functionality: "lazy" regexes, backtracking, named capture groups, and recursive patterns, all of which are powerful additions to POSIX BRE/ERE. (See lazy matching below.)