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

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

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 Python, please visit: https://docs.python.org/3/library/re.html