Regular Expressions for Beginners: 10 Patterns You Will Use
Learn regex through ten practical patterns: digits, loose email checks, trimming spaces, reformatting dates, plus greedy vs lazy matching and escaping special characters.

Regular expressions look like line noise at first: ^\s+|\s+$ does not exactly explain itself. But most everyday regex work uses a small set of building blocks, and once you know ten patterns you can read and adapt most of what you find online. Open the regex tester next to this article and try each pattern as you go: matches are highlighted as you type, and the replace preview shows the result before you touch real data.
The building blocks in one minute
\da digit,\wa letter, digit or underscore,\sany whitespace (space, tab, newline)..any single character (except a newline, unless you use thesflag).[abc]one of these characters,[a-z]a range,[^0-9]anything except these.+one or more,*zero or more,?optional,{3}exactly three,{2,4}two to four.^start and$end of the text (or of each line with themflag).( )a group you can reuse in the replacement as$1,$2.
The examples below use JavaScript syntax, which is what the tester runs. Python, PHP and most editors understand nearly all of it.
10 patterns worth knowing
- A number:
\d+finds “42” and “2026” in “Order 42 from 2026”. With thegflag you get all of them. - Exactly five digits, such as a postcode field:
^\d{5}$. Without^and$, “123456” would also pass, because it contains five digits. - An email, loosely:
^[^@\s]+@[^@\s]+\.[^@\s]+$. Something, an @, something, a dot, something, no spaces. That is enough to catch typos. A “perfect” email regex is hundreds of characters long and still wrong in places; the only real check is a confirmation email. - Leading and trailing spaces:
^\s+|\s+$, replaced with nothing. The|means “or”. Use thegflag so both ends are cleaned. - Several spaces in a row:
{2,}(a space before the braces), replaced with a single space. Good for text pasted from PDFs. - Reformat a date:
(\d{2})\.(\d{2})\.(\d{4})with the replacement$3-$2-$1turns “24.12.2026” into “2026-12-24”. The groups capture day, month and year, and the replacement puts them in a new order. Note that it checks the shape, not the calendar: “99.99.2026” matches too. - A whole word:
\bcat\bmatches “cat” but not “category” or “concat”.\bis a word boundary. - One of several options:
\.(jpe?g|png|webp)$with theiflag matches file names ending in .jpg, .JPEG, .png or .webp. - An optional letter:
colou?rmatches both “color” and “colour”. The?applies to the character just before it. - Lines that start with something:
^ERROR.*$with themflag finds every line of a log that begins with “ERROR”.
Greedy vs lazy
By default, + and * are greedy: they take as much text as they can. Try <.+> on <b>bold</b> text. You might expect it to match <b>, but it matches <b>bold</b>, from the first < to the last >.
Adding a question mark makes the quantifier lazy: <.+?> stops at the first possible > and matches <b> and then </b> separately. An even clearer option is a negated class: <[^>]+> means “a <, then anything that is not a >, then a >”. It is faster and says exactly what you mean.
Escaping special characters
These characters have a special meaning: . * + ? ^ $ ( ) [ ] { } | \ /. To match one literally, put a backslash before it.
3.5matches “3.5”, but also “3x5” and “325”, because the dot means any character. Use3\.5.- A price like “$19.99”:
\$\d+\.\d{2}. - Text in brackets, such as “(draft)”:
\(draft\).
Inside square brackets most characters lose their special meaning, so [.] is also a literal dot. When you put a pattern inside a string in code, you may need to double the backslashes ("\\d+"), because the string itself uses backslashes too.
Our take
Build patterns in small steps. Start with the part you are sure about, check the highlights, then add the next piece. Always test with text that should not match as well as text that should, because most regex bugs are patterns that match too much. Before running a replace on a real file, keep a copy. And resist the urge to solve everything in one expression: two simple patterns that anyone on the team can read beat one clever line nobody dares to touch.
Anthony has worked in IT for more than 17 years: office and field networks, servers, telephony, user support and security patching in international organisations. He writes about networking, security, developer tools and calculators.




