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
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • 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

/
/
gm

Test String

Code Generator

Generated Code

$re = '/^#(.*)((?:.|[\n])+?)(?=^#|\Z)/m'; $str = ' # 1、孔子语录(论语) 子曰:“学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?” 子曰:“温故而知新,可以为师矣。” 子曰:“学而不思则罔,思而不学则殆。” 子曰:“由,诲女知之乎?知之为知之,不知为不知,是知也。” 子贡问曰:“孔文子何以谓之‘文’也?”子曰:“敏而好学,不耻下问,是以谓之‘文’也。” 子曰:“默而识之,学而不厌,诲人不倦,何有于我哉?” 子曰:“三人行,必有我师焉。择其善者而从之,其不善者而改之。” 子曰:“知之者不如好之者,好之者不如乐之者。” 子在川上曰:“逝者如斯夫,不舍昼夜。” 子曰:“吾尝终日不食,终夜不寝,以思,无益,不如学也。” # 2、鱼我所欲也(《孟子·告子上》)   鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也。生,亦我所欲也,义,亦我所欲也,二者不可得兼,舍生而取义者也。生亦我所欲,所欲有甚于生者,故不为苟得也。死亦我所恶,所恶有甚于死者,故患有所不辟也。如使人之所欲莫甚于生,则凡可以得生者何不用也。使人之所恶莫甚于死者,则凡可以辟患者何不为也!由是则生而有不用也,由是则可以辟患而有不为也。是故所欲有甚于生者,所恶有甚于死者。非独贤者有是心也,人皆有之,贤者能勿丧耳。   一箪食,一豆羹,得之则生,弗得则死。呼尔而与之,行道之人弗受;蹴尔而与之,乞人不屑也。   万钟则不辩礼义而受之,万钟于我何加焉!为宫室之美,妻妾之奉,所识穷乏者得我与?乡为身死而不受,今为宫室之美为之;乡为身死而不受,今为妻妾之奉为之;乡为身死而不受,今为所识穷乏者得我而为之;是亦不可以已乎?此之谓失其本心。 # 3、生于忧患,死于安乐(《孟子·告子下》)   舜发于畎亩之中,傅说举于版筑之间,胶鬲举于鱼盐之中,管夷吾举于士,孙叔敖举于海,百里奚举于市。   故天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,曾益其所不能。   人恒过,然后能改;困于心,衡于虑,而后作;征于色,发于声,而后喻。入则无法家拂士,出则无敌国外患者,国恒亡。然后知生于忧患而死于安乐也。'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php