There are some exceptions to the basic rule that prefix words do not include the hyphen:
Root word is capitalized. Root word is a date. Hyphenate to avoid ambiguity. Root consists of two words. Root and prefix share identical letters. Prefix stands alone. Prefix is ex- or quasi-.
Root Word Capitalized
Hyphenate the prefix word if the root word is capitalized:
un-American post-Keynesian pre-Roe era
Root Word Is a Date
Hyphenate the prefix word if the root word is a date:
Hyphenate the prefix word to avoid an ambiguity:
re-cover (not recover) un-ionized (not unionized) pre-judicial career (not prejudicial career) re-create (not recreate)
Root Consists of Two Words
Hyphenate the prefix word if the root consists of two words. If those two words are themselves hyphenated, retain that hyphen:
pre-Civil War days non-English-speaking visitors (Retain hyphen in English-speaking.)
Hyphenate the prefix word if the last letter of the prefix and the first letter of the root word are the same. This is optional, and many writers do not take that option:
co-owner anti-intellectual non-native post-trial reengineer (Patent lawyers routinely omit the hyphen.) reexamination (Ditto.)
Prefix Stands Alone
Hyphenate the prefix when presenting two or more expressions and a prefix stands alone.
macro- and microeconomics
pre- and posttrial strategies or pre- and post-trial strategies (optional hyphen under the shared-letter rule)
Prefix Is ex- or quasi-
Hyphenate the prefix word if the prefix is ex- or quasi-:
Note: As a matter of style, it's best to use former instead of the prefix ex-.
Thus: former mayor.