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 = '/^\w+/m'; $str = ' mute unmute purchase mate material property prototype validate verify checking attribute constructor refresh empty enable allow 开启允许 format calculate example prior adj.优先地 priority n.优先事项 prime adj.最好的,首要的(优质的)。 prime beef 最好的牛肉,优质的牛肉 primitive n.原始的, 落后的 primary adj.最初的, 首要的。primary school 小学 prince 王子 princess 公主 principal 校长 principle 原则。王子公主校长都是有原则的人 permission premium premier 第一的,首相 preference china Chinese Chain engineer default defined north norwegian preset preference profession property profession reference release chore distinct bridge (dge单词) budget destroy (oy, oi) toilet depot depart department unique gen generator general universal multiple shift shit force quit quiet quick accumulate calculate ascend descend version ocean because course (课堂) bottle kettle water watch weather winter windy profile 人物简介 avatar 头像 fame movie clips band husband hand hair head lead register login sign sign up sing single signal symbol content context contest contact contract contrast container column microsoft square feature future challenge winter windy price precious tree stress scheme fresh refresh release execute manual filter south source 南方的资源 sound 南方的声音 graph machine This machine does not support recommend panel fold unfold expand spread response browse add subtract multiply divide finish finished infinity thunder autumn column wolf word world war wardrobe focus umbrella number true truth picture 图tu kitchen chicken 鸡穿ck chair 厨房有椅子和空气 night 干活gh daughter doctor actor monday money monkey memory garden sugar 公园里有糖(白糖) sweet 形容词 biscuits cookie coffee office street tree 街上有一棵树 matter letter bread breath 会呼吸的面包 soap soup 汤里有肥皂 lock clock 锁住一个时钟 road abroad 国外的公路 board chalk chocolate 粉笔巧克力 beat beast 野兽的节奏 best 最好的节奏 count discount 计算折扣 contain captain certainly 队长肯定包含 rise rice rich 升起的大米很有钱哦 explode explorer 爆炸后就要进去探索 replace repeat 替换重复的 execute excuse 执行道歉 every evening 每一个晚上 everything 每一个晚上都在想每一个事 test text 测试文本 device two town 两个小镇 better battle 最好的战斗 completely group grow 生成的小组 foot food 用脚踢食物 collection college 收集大学(大专) universe university 宇宙大学(综合大学) plugins small mall 很少的商店 night right 最右的晚上 yourself ourselves sel seven eleven give 给东西 get 获得东西 tuesday 星期二吃兔子 thursday then them 然后他们 analysis 可数名词 analyses 复数形式 analyse 动词(英式) analyze 动词(美式) analyses 动词三单 bounce debounce 跳起来就能去抖动 throttle threshold 掐死一个门阀 terminal 终端,航空站 exam example 考试用例 normal normalize 正常的规范化 house horse 我的马最多能跑两个小时 pump pumpkin 抽水机像一个南瓜 speed spend 花钱的速度很快 lucky 路上有一条ck discussions italian ta在意大利lian爱了 village 村庄生病ill的老人很多age valley 全部all山谷都有鳄鱼ey act action active 行动很积极 activity -e+ity '; 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