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
No Match

r"
"
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"(开发|开|发|电子|普|增)票" test_str = ("可以开发票吗?\n" "我想开发票。\n" "没有了,谢谢啊,嗯,拜拜诶,我想开发票,怎么弄啊。\n" "我这边要打的到这个套房是不是开车发票自己再回来是不是我只能说是打的非常的一个。\n" "发票。\n" "买火车票。\n" "嗯,您好,我是要要零要我想问一下那个前台可以,就是可以买火车票吗?\n" "票。\n" "嗯,那个明天你们这发票是不可以二十四小时开发票。\n" "我想要个电子票。\n" "开发票。\n" "厅车票。\n" "嗯,开发票。\n" "六七二号房的怎么老票灯老是自己票面的。\n" "工作室社保,我转接我不知我的保定发票方向了。\n" "发票。\n" "开发票。\n" "我要开发票。\n" "那个之后抱抱房卡还是要前台去哪那个餐票?\n" "喂,那个重新叫餐,可以开发票吗?\n" "说理的机票了。\n" "完票电视。\n" "额,我说我这会退票,呃退房要去机场,如果需要查房,你那服务员现在上来啊。\n" "我要开发票。\n" "开发票。\n" "我想要开发票。\n" "六六六开票。\n" "我票不漂亮。\n" "嗯,订餐是怎么订的,可以开发票吗?\n" "我要开发票。\n" "小票。\n" "我看发票。\n" "那个开票。\n" "酒店可以开发票吗?\n" "嗯,再拿两个酒店超水平差票可以吗,水晶消毒液吗?\n" "我现在房间点餐的话呢,那个开发票该怎么操作?\n" "嗯,帮我打印发票。\n" "车票?\n" "下我问问一下这个开票的事情吗。\n" "嗯,就是我就是明天早上不是用今天晚上入住吗?明天早上退房的时候不是开票吗?我就想一下开票的时候能不能不备注这个入住时间了。\n" "吃早餐用法还是票。\n" "就是在这个时候,然后到说道开票。\n" "我要开发票。\n" "打扫房间的时候,我就一张车票,现在不见了,请问一下打扫房间的到我的车票吗?\n" "问一下开发票的事情。\n" "有订票服务,没有。\n" "给我开个发票。\n" "我要开票。\n" "你是走个票一下。\n" "我要开发票。\n" "我要开发票。\n" "那个房间的灯所有的灯都不会凉了电源板一直在呼叫在不在票。\n" "票。\n" "帮我送俩门票,就是。\n" "嗯,我楼上不知道是不是幺八零七有客人一直在楼上票。\n" "是发票。\n" "一下我过了十点可以先提前帮我开票什么吗?\n" "我想开发票。\n" "我要开发票。\n" "开发票。\n" "我要开发票。\n" "哎,你好,我想问一下能代买那个高铁票吗?\n" "嗯,购买高铁票。\n" "开票。\n" "现在不开票,我把那个可能的行程给他看了。\n" "你好,帮我记一下,总票吧。\n" "开票。\n" "开发票。\n" "我开发票。\n" "酒精棉票。\n" "窗式发票,我想一个房间大不大。\n" "我要开发票。\n" "我要开发票。\n" "帮我定个发票。\n" "开票。\n" "我不要开发票。\n" "我要开发票。\n" "嗯,我要开发票。\n" "我要开发票。\n" "今天的股票行情是怎么样?\n" "发票吗?上野上不了不出来,这个什么呢?\n" "开票。\n" "我要开发票。\n" "我开发票。\n" "开发票。\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