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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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"(^去机场$)|接送(机|服务)|送(到)?机(场|服务)" 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" "哦,我这个电视我要连接usb播放我手机的视频是怎么操作的,我这边不会弄,你可以叫人上来帮我弄吗?\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