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 = '/^(?<MasterGroup>(?<MatchMisc>.{0,8}?(刚刚|烫手|热乎|出息|来[了啦辣喽]).{0,8}+)|(?<StrictMatchNumber>\d)|(?<MatchMost>.{0,8}?(?<PreQuantifier>[后前]?)第?(?<NumberMatch>[0-9一二三四五六七八九十两俩百万千亿半]+[kw]?)(?<PostQuantifierMatch>[后前个条发第]?(?<PostQuantifierMost>点?赞|播放?|弹幕|看|转发?|开|分钟?|小?时|秒|min|h)|(?<CompareMatch>[以之]?[后前])).{0,8}+)|(?<MatchOrder>.{0,8}?[前第](?&NumberMatch).{0,8}+)|(?<MatchInverse>.{0,8}?[前第](?&NumberMatch)[个名].{1,8}+)|(?<MatchAnchor>空降.{3,8}+))$/m'; $str = '(注。以下测试样例部分取自某些视频的真实弹幕,少部分是自己编的句子) 看到我就是两百以后了 看到我你就不是第一了 1播放,256赞,B站特色 一分钟!!!312个赞 两分钟热热乎乎 600播放500赞 第一名是我 第838个赞我收下了 天哪那,第四个转发!!! 4分钟 热乎 183条弹幕 十分钟的,还热乎啊,真香 关上前500发弹幕的大门 怎么过一会儿就18个转发了 关上前10000播放量的大门 热乎25min 8888点赞 12个 第4 来了来了,1分钟! 热乎 前2000 我迟到了10min!!! 有10只 2分钟,烫 2021第一天就这么刺激 借刀杀人*2 2333333 66666666 2 1 2:42 出息了 7分钟 很快啊! 一番定夺 第一哈哈哈 半小时,冻死了 空降00:00 牛顿第二定律 你这到底是保护村庄还是破坏村庄来了 燃起来了! 来了来了 很残酷的事实:在未命名的情况下不管你杀不杀末影螨它都会在两分钟后消失 (以下是误杀的例子。注意,这一些误杀并不准备修复,毕竟正则是读不懂人话的。) 三分钟后会消失 (。。。我找不到误杀的例子了。。。) 刚刚,放进岩浆里还能吃 (哇这个竟然没有屏蔽!想办法修复中哇。。。)'; 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