Regex

This text is a stub.Our community needs more information on this page! Can you help out?

## Blacklists

Search for a word
`$ egrep "serendipitous" foobar >hits`
If you only write "serendipitous", it'll search for each letter independently. If you want to find the word, you need to use a tool like egrep (extended grep).
Search and replace a word
`$ sed 's/serendipity/fortuitous/g' foobar >hits`
Find all instances of "serendipitous" in the file "foobar", and replace them with the word "fortuitous".
`sed` stands for stream editor.
The `s` operator (substitute) at the beginning, tells the interpreter to substitute one pattern for another.
`g` (global) tells it to do so as many times as possible on a line.
Single-character metacharacters
Some metacharacters match single characters.
`.` matches any one character. Fine if we aren't particular about the character we match (a number, letter, space or underscore).
`[...]` matches any character listed between the brackets
`[^...]` matches any character listed between the brackets.
`$ egrep "blurfle." Important.files >blurfles`
In a file called "Important.files", we want to grep out filenames following the pattern *blurfle1, blurfle2, blurfle3*, etc. But exclude files following the pattern *1blurfle, 2blurfle*, etc.
Character class
Matches any character listed within that class, and are separated off using square brackets.
If we wish to narrow the field of choices for a single character match.
`$ egrep "blurfle[0123456789]" Important.files >blurfles`
Match on "blurfle" but only when it is followed immediately by a number.
This can be abbreviated: `[0123456789]` → `[0-9]`.
You can also search for "blurfle" followed by any alphanumeric character: `[0-9A-Za-z]`. Notice that we didn't write "[0-9 A-Z a-z]", because the spaces might make it easier to read, but we'd be matching on anything between the ranges, or a space.
Add a carat to negate that class. In other words, if you wanted to find all instances of blurfle except those which end in a number, you'd use the following: `[^0-9]`
Quantifiers
In the last example, we were trying to search for patterns that contain "blurfle" followed by a number between zero and nine. The regex we came up with would match on blurfle1, blurfle2, blurfle3, etc. If, however, you had a programmer who mistakenly thought that "blurfle" was supposed to be spelled "blurffle," our regex wouldn't be able to catch it. We could fix it, though, with a quantifier.
`$ egrep "blur[f]+le[0-9]" Important.files >blurfles` Here we have "Find 'b', 'l', 'u,' 'r' (in a row) followed by one or more instances of an 'f' followed by 'l' and 'e' and then any single digit character between zero and nine."
`.\*` is an ubiquitous (and exceedingly powerful) combination. If we use single-character metacharacters and quantifiers in conjunction with one another, we can search for almost all the variant spellings of "blurfle" ("bllurfle," "bllurrfle", bbluuuuurrrfffllle", and so on).
`$ egrep "b.\*e" Important.files >blurfles` If we work this out, we come out with something like: "find a 'b' followed by any character any number of times (including zero times) followed by an 'e'." It's tempting to use ".\*" with abandon. However, bear in mind that the preceding example would match on words like "blue" and "baritone" as well as "blurfle."
`$ egrep "blurfle[0-9]{3}" Important.files >blufles` Suppose the filenames in blurfle are numbered up to 12324, but we only care about the first 999. This regex tells egrep to match any number between zero and nine exactly three times in a row. Similarly, "blurfle[0-9]{3,5}" matches any number between zero and nine at lest three times but not more than five times in a row.
Anchoring
`$ egrep "blurfle[0-9]" Important.file >script.sh`
Escape characters
So, we know that a regex like ".\*" finds any character any number of times. But suppose we're searching for ellipses of various lengths and we just want to find periods any number of times. Because the period is normally a special character, we'd need to escape it with a backslash:
`$ grep "\.\*" Important.Files >ellipses.files`
Alteration
`$ egrep "blurfle|blurffle" Important.files >blurfles` finds either *blurfle* or *blurffle*.
`$ egrep "gr(a|e)y" Important.files >hazy.shades` limits the scoppe of alternative matches. This accounts for both the American and English spellings of "gray".
Or perhaps a mail-filtering program that uses the following regex to single out past correspondence between you and the boss: `/(^To:|^From:) (Seaman|Ramsay)/`. This says, "Find a 'To:' or a 'From:' line followed by a space and then either the word 'Seaman' or the word 'Ramsay'.

## Glossary

Regular expression (abbr. regex)
Sets of symbols and syntactic elements used to match patterns of text.
Used in all kinds of text-manipulation tasks. Search-and-replace are among the most common uses.
Drawbacks: Easier to write than to read. Macro scripts have better readability, and is thus easier to use in collaborations.
Regex rules
Alphanumeric characters match themselves.
Metacharacters
Characters that match in a more generalized fashion.
Symbol characters or syntactic elements that function as search operators in the syntax.
Macros
Many regex implementations have macros for various classes.
In Perl, for example, \d matches any digit ([0-9]) and \w matches any "word character" ([a-zA-Z0-9\_]).
Grep uses a slightly different notation for the same thing: [:digit:] for digits and [:alnum:] for alphanumeric characters. The man page (or other documentation) for the particular tool should list all the regex macros available for that tool.
Anchoring
Specify the position at which a particular pattern occurs.
Escape characters
If one of the special characters like `\*` and `?` are a part of the actual string you want to search for.
Reverses the meaning of a special character, in other words, to treat it as a normal character instead of as a metacharacter.
Backreferences
Perl and regex
## Operators

Wildcard operators
Examples `\*` and `?`
Operators
`-` Range operator
`^` (Carat) Negation operator
`\` (Backslash) Escape character
`|` Logical *OR*
`(|)` (Parenthesis) to limit the scope of alternative matches. This can make your regex's extremely flexible, but be careful! Parentheses are also metacharacters which figure prominently in the use of ...
Alphanumeric character
Either upper or lower case.
`[A-Za-z]`
Quantifiers
Metacharacters which specify the number of times a particular character should match.
`?` matches any character zero or one times
`\*` matches the preceding element zero or more times
`+` matches the preceding element one or more times
`{num}` matches the preceding element *num* times
`{min,max}` matches the preceding element *min* times, but not more than *max* times.
These metacharacters allow you to match on a single-character pattern, but then continue to match on it until the pattern changes.
Anchors
`^` matches at the start of the line.
`$` matches at the end of the line.
`\<` matches at the beginning of a word.
`\>` matches at the end of a word.
`\b` matches at the beginning or end of a word.
`\B` matches any character not at the beginning or end of a word.
`^` and `$` are some of the most useful metacharacters in the regex arsenal.

## Syntax

`#` for comments
`.\*` is an ubiquitous (and exceedingly powerful) combination. If we use single-character metacharacters and quantifiers in conjunction with one another, we can search for almost all the variant spellings of "blurfle" ("bllurfle," "bllurrfle", bbluuuuurrrfffllle", and so on).
`\` escape to treat metacharacters as a regular character (string)
Calculation operators
`+` the character in front can occur more than once. For example `ab+c` will match both *"abc, aabbc, abbbbbbccc"*, but will not match "aboc, abbac, abk".
Logical operators
`|` Logical *OR*
`(|)` (Parenthesis) to limit the scope of alternative matches. This can make your regex's extremely flexible, but be careful! Parentheses are also metacharacters which figure prominently in the use of ...
Wiki Attributes
`<autoconfirmed>` only non-autoconfirmed users are unable to create/upload/move such pages
`<noedit>` users are also unable to edit this article
`<casesensitive>` don’t ignore case when checking title for being blacklisted
`<errmsg>` the name of the message that should be displayed instead of standard
You can use `|` to divide between multiple attributes in the same enclosed element, like so: `<autoconfirmed|noedit>`
## Read more

* https://web.archive.org/web/20050213032350/http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html
* https://www.mediawiki.org/wiki/Extension:TitleBlacklist#Resources\_on\_regular\_expressions