Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
No Match

@"
"
gmi

Test String

Code Generator

Generated Code

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(1\\.\\d+)\\D+?(?:(\\S+)\\s+)+?(\\d+)"; final String string = " BRIEF CONTENTS\n" + " Chapter1 Basic Concepts 1\n" + " Chapter2 Resistive Circuits 24\n" + " Chapter3 Nodal and Loop Analysis Techniques 89\n" + " Chapter4 Operational Amplifiers 147\n" + " Chapter5 Additional Analysis Techniques 171\n" + " Chapter6 Capacitance and Inductance 219\n" + " Chapter7 First- and Second-Order Transient Circuits 252\n" + " Chapter8 AC Steady-State Analysis 305\n" + " Chapter9 Steady-State Power Analysis 362\n" + " Chapter10 Magnetically Coupled Networks 411\n" + " Chapter11 Polyphase Circuits 450\n" + " Chapter12 Variable-Frequency Network Performance 482\n" + " Chapter13 The Laplace Transform 543\n" + " Chapter14 Application of the Laplace Transform to \n" + " Circuit Analysis 569\n" + " Chapter15 Fourier Analysis Techniques 617\n" + " Appendix Complex Numbers 659\n" + " vi\n" + " FM.indd viFM.indd vi 21/11/14 11:11 AM21/11/14 11:11 AM\n" + " CONTENTS\n" + " Preface ix 5.3 Thévenin’s and Norton’s Theorems  179\n" + " Chapterone 5.4 Maximum Power Transfer  197\n" + " Summary  202\n" + " Basic Concepts 1 Problems  202\n" + " 1.1 System of Units 2 Chaptersix \n" + " 1.2 Basic Quantities 2 Capacitance and Inductance 219\n" + " 1.3 Circuit Elements  8\n" + " Summary  17 6.1 Capacitors  220\n" + " Problems  18 6.2 Inductors  227\n" + " 6.3 Capacitor and Inductor Combinations  236\n" + " Chaptertwo Summary  241\n" + " Resistive Circuits 24 Problems  241\n" + " 2.1 Ohm’s Law  25 Chapterseven \n" + " 2.2 Kirchhoff’s Laws  30 First- and Second-Order Transient Circuits 252\n" + " 2.3 Single-Loop Circuits  38\n" + " 2.4 Single-Node-Pair Circuits  45 7.1 Introduction  253\n" + " 2.5 Series and Parallel Resistor Combinations  50 7.2 First-Order Circuits  254\n" + " 2.6 Wye Delta Transformations  59 7.3 Second-Order Circuits  275\n" + " 2.7 Circuits with Dependent Sources  63 Summary  289\n" + " Summary  68 Problems  289\n" + " Problems  69 Chaptereight \n" + " Chapterthree AC Steady-State Analysis 305\n" + " Nodal and Loop Analysis Techniques 89 8.1 Sinusoids  306\n" + " 3.1 Nodal Analysis  90 8.2 Sinusoidal and Complex Forcing Functions  309\n" + " 3.2 Loop Analysis  111 8.3 Phasors  312\n" + " Summary  128 8.4 Phasor Relationships for Circuit Elements  314\n" + " Problems  129 8.5 Impedance and Admittance  318\n" + " 8.6 Phasor Diagrams  325\n" + " Chapterfour 8.7 Basic Analysis Using Kirchhoff’s Laws  328\n" + " Operational Amplifiers 147 8.8 Analysis Techniques  331\n" + " Summary  344\n" + " 4.1 Introduction  148 Problems  344\n" + " 4.2 Op-Amp Models  148\n" + " 4.3 Fundamental Op-Amp Circuits  154 Chapternine \n" + " Summary  163 Steady-State Power Analysis 362\n" + " Problems  163 9.1 Instantaneous Power  363\n" + " Chapterfi ve 9.2 Average Power  364\n" + " Additional Analysis Techniques 171 9.3 Maximum Average Power Transfer  369\n" + " 9.4 Effective or rms Values  374\n" + " 5.1 Introduction  172 9.5 The Power Factor  377\n" + " 5.2 Superposition  174 9.6 Complex Power  379\n" + " vii\n" + " FM.indd viiFM.indd vii 21/11/14 11:11 AM21/11/14 11:11 AM\n" + " viii CONTENTS\n" + " 9.7 Power Factor Correction  384 13.3 Transform Pairs  547\n" + " 9.8 Single-Phase Three-Wire Circuits  388 13.4 Properties of the Transform  549\n" + " 9.9 Safety Considerations  391 13.5 Performing the Inverse Transform  551\n" + " Summary  399 13.6 Convolution Integral  557\n" + " Problems  399 13.7 Initial-Value and Final-Value Theorems  560\n" + " 13.8 Solving Differential Equations with Laplace \n" + " Chapterten Transforms  562\n" + " Magnetically Coupled Networks 411 Summary  564\n" + " 10.1 Mutual Inductance  412 Problems  564\n" + " 10.2 Energy Analysis  423 Chapterfourteen \n" + " 10.3 The Ideal Transformer  426 Application of the Laplace Transform to \n" + " 10.4 Safety Considerations   436 Circuit Analysis 569\n" + " Summary  437\n" + " Problems  438 14.1 Laplace Circuit Solutions  570\n" + " 14.2 Circuit Element Models  571\n" + " Chaptereleven 14.3 Analysis Techniques  573\n" + " Polyphase Circuits 450 14.4 Transfer Function  586\n" + " 11.1 Three-Phase Circuits  451 14.5 Steady-State Response  603\n" + " 11.2 Three-Phase Connections  456 Summary  606\n" + " 11.3 Source/Load Connections  457 Problems  606\n" + " 11.4 Power Relationships  466 Chapterfi fteen \n" + " 11.5 Power Factor Correction  471 Fourier Analysis Techniques 617\n" + " Summary  475\n" + " Problems  475 15.1 Fourier Series  618\n" + " 15.2 Fourier Transform  641\n" + " Chaptertwelve Summary  651\n" + " Variable-Frequency Network Performance 482 Problems  651\n" + " 12.1 Variable Frequency-Response Analysis  483\n" + " 12.2 Sinusoidal Frequency Analysis  491 Appendix\n" + " 12.3 Resonant Circuits  500 Complex Numbers 659\n" + " 12.4 Scaling  521\n" + " 12.5 Filter Networks  523 Index 666\n" + " Summary  534\n" + " Problems  535\n" + " Chapterthirteen \n" + " The Laplace Transform 543\n" + " 13.1 Definition  544\n" + " 13.2 Two Important Singularity Functions  544\n" + " FM.indd viiiFM.indd viii 21/11/14 11:11 AM21/11/14 11:11 AM"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html