MS Word Regex

MS Office has a handy Word regex search and replace. Below are listed most common regex options

Basic Regex used in Word

? and *

The two most basic wildcards are ? and *. They are essentially similar in use.
? is used to represent a single character and * represents any number of characters. On their own, these have limited use.
s?t
 will find satset,sit ,sot and any other combination of three characters beginning with ‘s‘ and ending with ‘t‘. It will also find that combination of letters with a word, thus it would locate the relevant (highlighted) part of inset etc.

@

@ is used to find re-occurrences of the previous character (if any). e.g. lo@t will find lot or lootful@ will find ful or full etc.

< >

With any of the above (or any other combination of wildcards and characters), you can use the brackets < and > to mark the start and end of a word respectively. Thus in the example used above for ‘*
<s*t> would find ‘secret‘ and ‘serpent‘, but not ‘sailing boats‘ and ‘sign over documents‘. Though again, given the use of ‘*‘, beware as it will find the block of text from a word starting with ‘s’ to the end of the next word in the document ending with ‘t‘, e.g. ‘sailing boat‘ which may not be what you had in mind.
The <> brackets can be used in pairs as above or individually as appropriate e.g. ful@>  will find ‘full‘ and the appropriate part of ‘wilful‘ but not ‘wilfully

[]

Square brackets are always used in pairs and are used to identify specific characters or ranges of characters. e.g.:
[abc] will find any of the letters abc[F] will find upper case ‘F‘;
[A-Z] will find any upper case letter; [0-9] will find any single number; [13579] will find any odd numbers; [0-9A-Za-z] will find any numbers or letters.
The characters can be any character or series of characters, including space. Characters are processed in order – lowest first. If you are uncertain which character is lower than another check with ‘Insert > Symbol’.

[!]

[!] is very similar to [ ] except in this case it finds any character not listed in the box so [!o] would find every character except “o”.
You can use ranges of characters in exactly the same was as with [ ], thus [!A-Z] will find everything except upper case letters.

You can paste any (Unicode) character – unfortunately *not* characters from decorative (Symbol) fonts) – into your search expressions. So copying the first and last characters from the Greek or cyrillic subsets into a search:
[;-ώ] would match any Greek character α β γ δ ε …
<[Ё-ґ]@> matches any cyrillic word: Вы можете помочь мне? (“Can you help me please?”)
You can type in Unicode characters with the Alt-key (make sure NumLock is on, then hold down the Alt-key and type the numbers on the numeric keyboard). Since all characters from decorative fonts (Symbol-, Wingdings-fonts …) are kept in a special code page from &HF000 to &HF0FF, you can search for them with [Alt61472-Alt61695].

{}

Curly brackets are used for counting occurrences of the previous character or expression.
{n}         This finds exactly the number ‘n’ of occurrences of the previous character
{n,}  Finds at least the number ‘n’ occurrences.
{n,m}      Finds the number of occurrences from ‘n’ to ‘m’.
Note: The above examples employ a comma as a list separator {n,m} – for languages that use alternative list separators, substitute the local separator character (often a semi-colon {n;m}) as appropriate.
Counting can be used with individual characters or more usefully with sets of characters e.g. [deno]{4} will match done, node, eden) or bracketed groups: (ts, ){3} will match ts, ts, ts, .
(Unfortunately, there is no wildcard to search for “zero or more occurrences” in Word wildcard searches; [!^13]{0,} does not work).

( )

Round brackets have no effect on the search pattern, but are used to divide the pattern into logical sequences, where you wish to re-assemble those sequences in a different order during the replace – or to replace only part of that sequence. They must be used in pairs.
The partnering backslash character is used as a replacement string in conjunction with a number to indicate which pair of brackets numbered from the left is required e.g.
(John) (Smith) replaced by \2 \1 – note the spaces in the search and replace strings –  will produce Smith John or replaced by \2 alone will give Smith.

\

If you wish to search for a character that has a special meaning in wildcard searches – the obvious example being ‘?‘. then you can do so by putting a backslash in front of it: [\?] will find the question mark character ‘?
If you wish to find the backslash itself then you need to precede that with a backslash [\\].
The following is a list of the characters that have a special meaning in wildcard searches ( [ ] { } < > ( ) – @ ? ! * \ )

NOTES

You may wish to identify a character string by means of a paragraph mark . The normal search string for this would be ^p.
^p DOES NOT WORK in wildcard search strings! It must however be used in replacement strings, but when searching, you must look for the substitute code ^13.
Wildcard searches will also not find footnote/endnote marks – substitute ^2.
A-z would be expected to reproduce all the letters between A and z i.e. both upper case and lower case letters, which it does, but it reproduces all the characters from ASCII 65 to ASCII 122, and that block also includes the characters [ ] ` ^ _ /  Use A-Za-z instead.
The question mark ? is used to find individual characters. If used with curly brackets to define a range of characters eg
#?{1,3}#
 it will behave as an asterisk and find all the characters between the hash symbols.

Word Specific Control Codes used with the search/replace tool

Code Notes
^1 In-line picture
^2 Auto referenced footnotes
^5 Annotation mark
^9 Tab
^11 New line
^12 Page or Section break
^13 Paragraph break / ‘carriage’ return
^14 Column break
^19 Opening field brace (when field braces are visible)
^21 Closing field brace (when field braces are visible)
? Question mark
^? Any single character (not valid in the Replace box)
^- Optional hyphen
^~ Non-breaking hyphen
^^ Caret character
^# Any digit
^$ Any letter
^& Contents of ‘Find What’ box (Replace box only)
^+ Em dash (not valid in the Replace box)
^= En dash (not valid in the Replace box)
^u8195 Em Space Unicode character value search (not valid in the Replace box)
^u8194 En Space Unicode character value search (not valid in the Replace box)
^a Comment (not valid in the replace box) (Word 97-2000 only)
^b Section break (not valid in the replace box)
^c Replace with Clipboard contents (Replace box only)
^d Field
^e Endnote Mark (not valid in the Replace box)
^f Footnote Mark (not valid in the Replace box)
^g Graphic (In Line Graphics Only). In Word 2007 a forward slash / also appears to find in-line graphics. This appears to be an unintentional bug.
^l New line
^m Manual Page Break
^n Column break
^t Tab
^p Paragraph Mark –
^s Non-breaking space
^w White space (space, non-breaking space, tab); not valid in the Replace box
^nnn Where “n” is an ASCII character number
^32 and ^032 will both represent a space character, but ^147 will represent ô and ^0147 will represent “
^0nnn See above (Produces ASCII on Macintosh).
^unnnn Unicode character search where “n” is a decimal number corresponding to the Unicode character value.

Examples

Example 1 – Transpose first name and surname

There are many occasions when you are presented with blocks of text or numbers etc., where the order of the text is not what you might require in the final document. Swapping the placement of forename and surname as above is one such example – and don’t forget you can add to the replacement, even when using bracketed replacements e.g. you may wish John Smith to appear as Smith, John or, more likely, you may have a column of names in a table, where you wish to exchange all the surnames with all the forenames.

John Smith to become Smith John

You could do them one at a time, but by replacing the names with wildcards, you can do the lot in one pass.
Let’s then break up the names into logical sequences that can only represent the names.
At its simplest, we have here two words – John and Smith. They can be represented by <*>[space]<*> – where [space] is a single press of the spacebar.
Add the round brackets  (<*>)[space](<*>) and replace with \2[space]\1
Run the search on the column of names and all are swapped. Run it again and they are swapped back.

Example 2 – Transposing dates

This could be the changing of UK format dates to US format dates – or vice versa.
7th August 2001 to August 7th, 2001
To give an example of how most of the wildcards could be used in one search sequence to find any UK date formatted above to its equivalent US format date, the following search pattern will do the trick:

[0-9]{1,2}[dhnrst]{2} <[AFJMNSOD]*>[0-9]{4}

Breaking it down [0-9] looks for any single digit number, but dates can have two numbers so to restrict that to two, we use the count function. We want to find dates with 1 or 2 numbers so [0-9]{1,2}

Next bit is the ordinal ‘th’ – Ordinals will be ‘st’ ‘rd’ or ‘th’ so identify those letters specifically: [dhnrst]

There will always be two letters, so restrict the count to 2: [dhnrst]{2}

Next comes the space. You can insert a space [space]

The month always begins with one of the following capital letters – AFJMNSOD. We don’t know how many letters this month has so we can use the blanket ‘*‘ to represent the rest. And we are only interested in that word so we will tie it down with <> brackets: <[AFJMNSOD]*>

There’s another space [space] followed by the year. The years here have four numbers so [0-9]{4}

Finally add the round brackets to provide a logical breakup of the sequence:
([0-9]{1,2}[dhnrst]{2})[space](<[AFJMNSOD]*>)[space]([0-9]{4})
and replace with \2[space]\1,[space]\3
to re-order the sequence.

Example 3 – Adding or removing the period in salutations (Mr or Mr.)

Assume you are parsing addresses and wish to separate the honorific from the name. American usage puts a full stop (period) at the end (“Mr.”, “Mrs.”, “Dr.”) while British usage often omits the full stop.

([DM][rs]{1,2})( )

will find Mr Mrs Dr without the stop and

\1.\2

will put one in. Or vice versa

([DM][rs]{1,2}).

will find Mr. Mrs. Dr. with the stop and

\1

will take it out.

Example 4: Duplicate paragraphs (and rows)

(*^13)\1\1 will match any sequence of three identical paragraphs.
If you replace:
(*^13)\1
with
\1
it will delete all consecutive duplicate paragraphs in the document. Repeat until nothing is found, to delete all duplicate paragraphs in the document (as long as you have sorted the text first).
To delete duplicate rows in a table (provided you have no merged cells), you can convert the table to text (Table + Convert to Text, using a tab delimiter); delete the duplicate paragraphs using the above method, then convert the text back to a table.

Example 5: Tags

\<([!\<>]@>)*\</\1
will match any well-formed XML element including start-tag and end-tag such as:

<p>some text </p>

or

<customer-name>John Smith</customer-name>

Alternative way: In the Find What box, enter the following string:
\<*>
Leave the Replace With box empty.
Place an X in the “Use Wildcards” box.
Click Replace All.


Example 6: Formatting

By building up appropriate patterns, you can search for almost any combination of characters.
Of course you can also restrict your searches by specifying some style or formatting, or add formatting for the replacement text. See Finding and replacing non-printing characters (such as paragraph marks), other special characters, and text formatting for more on this.
A nice trick if you want to apply formatting to a part (but not all) of the search text is to put in “tags” in a first replacement.
In a find/replace, you can only change the formatting of the whole find-text; so you would need to do two find-replaces to get the job done. In the first, you would “tag” the text that has to be formatted; in the second, you format them (and remove the tags).

Find what:
(something)(something else)(another string)

Replace with:
\1$$\2##\3

and then remove the tags and apply the formatting in a second replace:

Find what:
$$(*)##

Replace with:
\1 ((bold))

Imagine, for instance, that you’ve got a text file, and headings are marked up by having 3 empty paragraphs before them and an empty paragraph after.

Find what:
^13{4}([!^13]@^13)^13

Replace with:
^p<H1>\1

Then Find that: 
\<H1\>(*) 

Replace with: 
\1 ((style “Heading 1”))

This will remove the empty paragraphs and format the headings.

BEWARE:

Sometimes Word will get confused if it encounters “escaped” brackets \( or \), for example “(\\)” will match *any* character, not only a backslash
Workaround: use “([” instead.

([a-z]\() throws an error – should find an “a(“.
Workaround: Use ([a-z][\(]) instead.

Not a bug but still annoying: You have to escape any special character even if you type its code; so ^92 will have the same problems as typing the backslash.

The construction {0,} (find zero or more of the preceding item) is refused as incorrect syntax. This concept is available in Unix regular expression matching, so it’s a curious omission.

You don’t always have to “escape” the special characters, if the context makes it clear that the special meaning isn’t wanted. [abc-] matches ““, and [)(] matches “)” or “(“. This may sometimes make your searches behave differently from what you expected.

More Examples – Greek

More Examples – Cyrrilic

Sources

RegEx

Intro

Regular expressions are a language of their own. When you learn a new programming language, they’re this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the “simple” pattern described. Today, we’ll review eight regular expressions that you should know for your next coding project.
Regular Expressions aka Regex are expressions that define a search pattern. They are widely used for validation purposes, like email validation, url validation, phone number validation and so on

Commonly used RegEx

Digits

Alphanumeric Characters

Email

Password Strength

  • Complex: Should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long
    /(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\-_+=~`|:;”‘<>,./?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}/
  • Moderate: Should have 1 lowercase letter, 1 uppercase letter, 1 number, and be at least 8 characters long
    /(?=(.*[0-9]))((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.{8,}$/

Username

  • Alphanumeric string that may include _ and – having a length of 3 to 16 characters –
    /^[a-z0-9_-]{3,16}$/

URL

  • Include http(s) Protocol /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)/
  • Protocol Optional /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/

IP Address

123456/* Match IPv4 address *//^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ /* Match IPv6 address *//(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))//* Match both IPv4, IPv6 addresses *//((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/

Dates

12345678910111213/* Date Format YYYY-MM-dd *//([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/ /* Date Format dd-MM-YYYY or dd.MM.YYYY ordd/MM/YYYYwith check for leap year *//^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/ /* Date Format dd-mmm-YYYY ordd/mmm/YYYY ordd.mmm.YYYY *//^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/

Time

HTML Tags

Javascript Handlers

Slug

  • Slug
    /^[a-z0-9]+(?:-[a-z0-9]+)*$/

Match Duplicates in a String

14. Phone Numbers

12/* International Phone Numbers *//^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/

Note: Use regex for validating phone numbers only if you don’t have the choice to use a library. There are several libraries that handle phone numbers more accurately and should be used instead.

File Path

Regex Tools

Resources

  • https://www.debuggex.com/
  • https://regex101.com/

RegEx Cheatheet

PCRE (Perl Compatible Regular Expressions) is a C library implementing regex. It was written in 1997 when Perl was the de-facto choice for complex text processing tasks. The syntax for patterns used in PCRE closely resembles Perl. PCRE syntax is being used in many big projects called flavors – .NET, Java, JavaScript, XRegExp, Perl, PCRE, Python, and Ruby, and the programming languages C#, Java, JavaScript, Perl, PHP, Python, Ruby, and VB.NET.
PCRE’s syntax is much more powerful and flexible than either of the POSIX regular expression flavors and than that of many other regular-expression libraries.
we’re focused on PRCE mostly unless stated!

Anchors

^
Start of string, or start of line in multi-line pattern
\A
Start of string
$
End of string, or end of line in multi-line pattern
\Z
End of string
\b
Word boundary
\B
Not word boundary
\<
Start of word
\>
End of word

Character Classes

\c
Control character
\s
White space
\S
Not white space
\d
Digit
\D
Not digit
\w
Word
\W
Not word
\x
Hexade­cimal digit
\O
Octal digit

POSIX

[:upper:]
Upper case letters
[:lower:]
Lower case letters
[:alpha:]
All letters
[:alnum:]
Digits and letters
[:digit:]
Digits
[:xdigit:]
Hexade­cimal digits
[:punct:]
Punctu­ation
[:blank:]
Space and tab
[:space:]
Blank characters
[:cntrl:]
Control characters
[:graph:]
Printed characters
[:print:]
Printed characters and spaces
[:word:]
Digits, letters and underscore

Assertions

?=
Lookahead assertion
?!
Negative lookahead
?<=
Lookbehind assertion
?!= or ?<!
Negative lookbehind
?>
Once-only Subexp­ression
?()
Condition [if then]
?()|
Condition [if then else]
?#
Comment

 

Groups and Ranges

.
Any character except new line (\n)
(a|b)
a or b
(…)
Group
(?:…)
Passive (non-c­apt­uring) group
[abc]
Range (a or b or c)
[^abc]
Not (a or b or c)
[a-q]
Lower case letter from a to q
[A-Q]
Upper case letter from A to Q
[0-7]
Digit from 0 to 7
\x
Group/­sub­pattern number “­x”
Ranges are inclusive.

Pattern Modifiers

g
Global match
i *
Case-i­nse­nsitive
m *
Multiple lines
s *
Treat string as single line
x *
Allow comments and whitespace in pattern
e *
Evaluate replac­ement
U *
Ungreedy pattern
* PCRE modifier

String Replac­ement

$n
nth non-pa­ssive group
$2
“­xyz­” in /^(abc­(xy­z))$/
$1
“­xyz­” in /^(?:a­bc)­(xyz)$/
$`
Before matched string
$’
After matched string
$+
Last matched string
$&
Entire matched string
Some regex implem­ent­ations use \ instead of $.

Hidden chars or shortcuts

\s = [ \t\n\r\f]
\d = [0-9]
\w = [a-zA-Z_0-9])

Quanti­fiers

*
0 or more
{3}
Exactly 3
+
1 or more
{3,}
3 or more
?
0 or 1
{3,5}
3, 4 or 5
Add a ? to a quantifier to make it ungreedy.

Escape Sequences

\
Escape following character
\Q
Begin literal sequence
\E
End literal sequence
“­Esc­api­ng” is a way of treating characters which have a special meaning in regular expres­sions literally, rather than as special charac­ters.

Common Metach­ara­cters

^
[
.
$
{
*
(
\
+
)
|
?
<
> ]
The escape character is usually \

Special Characters

\n
New line
\r
Carriage return
\t
Tab
\v
Vertical tab
\f
Form feed
\xxx
Octal character xxx
\xhh
Hex character hh

Case Conversion

\l      Make next character lowercase
\u     Make next character uppercase
\L     Make entire string (up to \E) lowercase
\U     Make entire string (up to \E) uppercase
\u\L Capitalize first char, lowercase rest (sentence)

 

PCRE regex quick reference

[abx-z] One character of: a, b, or the range x-z
[^abx-z] One character except: a, b, or the range x-z
a|b a or b
a? Zero or one a’s (greedy)
a?? Zero or one a’s (lazy)
a* Zero or more a’s (greedy)
a*? Zero or more a’s (lazy)
a+ One or more a’s (greedy)
a+? One or more a’s (lazy)
a{4} Exactly 4 a’s
a{4,8} Between (inclusive) 4 and 8 a’s
a{9,} 9 or more a’s
(?>…) An atomic group
(?=…) A positive lookahead
(?!…) A negative lookahead
(?<=…) A positive lookbehind
(?<!…) A negative lookbehind
(?:…) A non-capturing group
(…) A capturing group
(?P<n>…) A capturing group named n

 

^ Beginning of the string
$ End of the string
\d A digit (same as [0-9])
\D A non-digit (same as [^0-9])
\w A word character (same as [_a-zA-Z0-9])
\W A non-word character (same as [^_a-zA-Z0-9])
\s A whitespace character
\S A non-whitespace character
\b A word boundary
\B A non-word boundary
\n A newline
\t A tab
\cY The control character with the hex code Y
\xYY The character with the hex code YY
\uYYYY The character with the hex code YYYY
. Any character
\Y The Y’th captured group
(?1) Recurse into numbered group 1
(?&x) Recurse into named group x
(?P=n) The captured group named ‘n’
(?#…) A comment