Mega Code Archive

 
Categories / Ruby / String
 

Regular Expressions

Pattern             Description /pattern/options    Pattern pattern in slashes, followed by optional options, one or more of: i for case-insensitive; o for substitute once; x for ignore whitespace, allow comments; m for match multiple lines and newlines as normal characters. %r!pattern!         General delimited string for a regular expression, where ! can be an arbitrary character. ^                   Matches beginning of line. $                   Matches end of line. .                   Matches any character. \1...\9             Matches nth grouped subexpression. \10                 Matches nth grouped subexpression if already matched; otherwise, refers to octal representation of a character code. \n, \r, \t, etc.    Matches character in backslash notation. \w                  Matches word character; same as [0-9A-Za-z_]. \W                  Matches nonword character; same as [^0-9A-Za-z_]. \s                  Matches whitespace character; same as [\t\n\r\f]. \S                  Matches nonwhitespace character; same as [^\t\n\r\f]. \d                  Matches digit; same as [0-9]. \D                  Matches nondigit; same as [^0-9]. \A                  Matches beginning of a string. \Z                  Matches end of a string, or before newline at the end. \z                  Matches end of a string. \b                  Matches word boundary outside [] or backspace (0x08) inside []. \B                  Matches nonword boundary. \G                  Matches point where last match finished. [..]                Matches any single character in brackets, such as [ch]. [^..]               Matches any single character not in brackets. *                   Matches zero or more of previous regular expressions. *?                  Matches zero or more of previous regular expressions (nongreedy). +                   Matches one or more of previous regular expressions. +?                  Matches one or more of previous regular expressions (nongreedy). {m}                 Matches exactly m number of previous regular expressions. {m,}                Matches at least m number of previous regular expressions. {m,n}               Matches at least m but at most n number of previous regular expressions. {m,n}?              Matches at least m but at most n number of previous regular expressions (nongreedy). ?                   Matches zero or one of previous regular expression. |                   Alternation, such as color|colour. ( )                 Groups regular expressions or subexpression, such as col(o|ou)r. (?#..)              Comment. (?:..)              Groups without back-references (without remembering matched text). (?=..)              Specifies position with pattern. (?!..)              Specifies position with pattern negation. (?>..)              Matches independent pattern without backtracking. (?imx)              Toggles i, m, or x options on. (?-imx)             Toggles i, m, or x options off. (?imx:..)           Toggles i, m, or x options on within parentheses. (?-imx:..)          Toggles i, m, or x options off within parentheses. (?ix-ix: )          Turns on (or off) i and x options within this noncapturing group. [:alnum:]           class for alphanumeric. [:alpha:]           class for uppercase and lowercase letters. [:blank:]           class for blank and tab. [:cntrl:]           class for Control characters. [:digit:]           class for digits. [:graph:]           class for printable characters (but not space). [:lower:]           class for lowercase letter. [:print:]           class for printable characters (space included). [:punct:]           class for printable characters (but not space and alphanumeric). [:space:]           class for whitespace. [:upper:]           class for uppercase letters. [:xdigit:]          class for hex digits: A-F, a-f, and 0-9.