Skip to content

Regular Expressions Reference

A practical and portable regular expression reference for real-world use.
No flag syntax (e.g., (?s), (?m), (?i)) is used to ensure compatibility across OS and tools.
Only safe, environment-independent patterns are included.


OSNewline CodeRecommended PatternKey Point
Windows\r\n\r?\nUniversal pattern for CRLF/LF
macOS / Linux\n\r?\nCompatible across all platforms

Key Notes

  • Use \r?\n to safely detect or replace newlines on Windows/macOS/Linux.
  • To match blocks including newlines, use (?:.|\r|\n) since . alone excludes line breaks.

SyntaxMeaningExampleMatch
.Any single char except newlinea.c / abcabc
[abc]One of a, b, or cbaga
[^0-9]Any non-digita1a
[A-Z0-9]Uppercase letter or digitX8zX,8
SyntaxMeaningExampleMatch
*Zero or moregooooglegoooo
+One or moregoogleoo
?Zero or onecolor colourBoth
{n}Exactly n timesa{3} / aaaaaa
{n,}n or morea{2,} / aaaaaaaa
{n,m}Between n and ma{2,4} / aaaaaaaaa
SyntaxMeaningExampleMatch
^abcLine startabc\nzabcabc (line 1)
abc$Line endzabc\nabcBoth line ends
\bword\bWord boundaryword wordsword
\BingNon-boundaryringingsecond ing
SyntaxMeaningExampleMatch
\dDigit [0-9]ver2.102,10
\DNon-digita1a
\wAlphanumeric + _a_b-1a_b
\WNon-alphanumerica##
\sWhitespace (tab/newline etc.)a bspace
\SNon-whitespacea ba,b
\tTaba\tba[TAB]b
SyntaxMeaningExampleMatch
\.Literal dota.ca.c
\*Asteriska*ba*b
\+Plus signa+ba+b
\?Question markwhat?what?
\( \)Parentheses(test)(test)
|Pipe`ab`
\\BackslashC:\\path\\
\^Caret^abc^abc
\$Dollartotal$$
\[ \]Brackets[abc][abc]
\{ \}Braces{a,b}{a,b}

SyntaxPurposeExampleMatch
(abc)+Repeat as groupabcabcxabcabc
`(?:jpgpng)`Non-capturing ORfile.png
`foobar`OR conditionbar
\d+(?=円)Numbers before “円”合計100円100
^(?!.*error).*Line not containing “error”ok\nerrorok
(?<=¥)\d+Number after “¥”¥300300
(?<!Mr\.)\s[A-Z]Uppercase not after “Mr.”Ms. Alice A

Use CasePatternExampleMatch
HTML block`
(?:.
\r\n)*?`
Log entry`^[\d{4}-\d{2}-\d{2} [\d:]+](?:.\r\n)*?(?=^[\d{4}-\d{2}-\d{2}
Markdown code block“ ```(?:.\r\n)*?``` “
Comment (/*…*/)`/*(?:.\r\n)*?*/`

Part 4: Pattern Library (Filtering / Extraction)

Section titled “Part 4: Pattern Library (Filtering / Extraction)”
PurposePatternExampleMatch
Digits only^\d+$123123
Alphanumeric^[A-Za-z0-9]+$user01user01
Email (simple)^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$a@b.coma@b.com
URLhttps?://[\w.-]+\.[A-Za-z]{2,}(/[\w./?=&%-]*)?https://ex.com/ahttps://ex.com/a
ISO date\d{4}-\d{2}-\d{2}2025-10-302025-10-30
Intl. phone\+\d{1,3}[\s-]?\d{1,14}+81 90 1234 5678full
Strong password^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$Abcd1234full
HTML comment`<!—(?:.\r\n)*?—>`
Lines without “error”^(?!.*error).*ok\nerrorok

TaskPatternReplaceInputOutput
Reverse words(\w+)\s+(\w+)${2} ${1}John DoeDoe John
Change delimiter;,a;b;ca,b,c
Normalize spaces\s{2,} a ba b
Remove HTML tags<[^>]+><p>a</p>a
Trim spaces`^\s+\s+$`a
Newline → space\r?\n a\nba b
Remove end comment//.*$x=1;//notex=1;
Unify date format(\d{4})/(\d{2})/(\d{2})${1}-${2}-${3}2025/10/302025-10-30
Compress duplicate lines^(.*)(\r?\n\1)+$${1}repeated linessingle line

Regular expressions are a cross-platform and multilingual tool for text processing, scripting, log parsing, and data cleanup.
Using flag-independent, portable patterns ensures stability across diverse environments and editors.