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+\n(.*?\n)+?```(?!`)" test_str = ("---\n" "aliases: \n" "created: 2023-06-01 16:05:51\n" "grade: \n" "keys: \n" "modified: 2023-06-09 17:52:11\n" "tags: \n" "type: \n" "---\n" "# 设置波特率\n\n" "- 首先确定串口工作在什么模式, 因为不同的工作模式波特率的计算方式不一样, 这里拿 [[51单片机-串口通信相关的寄存器#工作方式 1 - 10 位异步收发器 (8 位数据) |串口工作方式 1]] 举例\n\n" "[串口工作方式 1的波特率计算](bookxnotepro://opennote/?nb={e2110889-ff80-422a-9be6-b93ea6c6a49f}&book=0b74da38c7c8b6eb08f99dbb49d646eb&page=194&x=66&y=387&id=7&uuid=0cca54394d67fa36000a6b0d501cd5b1) 我们最终设置的其实就是 TH1, 下面的 `定时器/计数器 1` 是一个东西\n\n" "![[Pasted image 20230601171852.png]] \n\n" "`STC90C51RC/RD+` 系列单片机的定时器有两种计数速率: 一种是 12T 模式, 每 12 个时钟加 1, 与传统 8051 单片机相同;另外一种是 6T 模式, 每 6 个时钟加 1, 速度是传统 8051 单片机的 2 倍, T0 的速率在烧录用户程序时在 STC-ISP 编程器中设置 \n\n" "- 我们设置单片机定时器工作在 12T 模式下, 那么公式就是 $2^{SMOD}/32*(定时器/计数器1的溢出率)$ \n" "- [185](bookxnotepro://opennote/?nb={e2110889-ff80-422a-9be6-b93ea6c6a49f}&book=0b74da38c7c8b6eb08f99dbb49d646eb&page=184&x=35&y=306&id=2&uuid=da0e635b80714c7cbe1c9566f2115627) 当 SMOD=1 时, 则使串行通信方式 1, 2, 3 的波特率加倍, SMOD=0 时, 则各工作方式的波特率加倍(说的有点莫名其妙, 直接实验就行了) \n" "- SYSclk 为系统时钟频率, 与用途有关, 比如说如果你要用到串口, 那么就常用 11.0592MHz 晶振 (设置串口波特率的时候误差是 0), 因为这个频率的晶振分频后可以得到精确的波特率, 如果要用到计时, 就常用 12MHz 晶振, 这样分频后是个整数, 计时更精确 ^axkydw\n\n" "# 示例程序\n\n" "## 串口的初始化步骤\n\n" "- 串口首先设置波特率, 波特率脉冲通过定时器产生, 所以第一步就是设置定时器确定波特率\n" " 1. 确定 T1 定时器的工作方式 (TMOD 寄存器), 设置 [[MOC-51单片机-中断#定时器/计数器的 4 种工作方式|工作模式]]为 8 位自动重装模式\n" " 2. 确定串口工作方式 (SCON 寄存器) \n" " 3. 计算 T1 的初值 (设定波特率), 装载 TH1, TL1\n" " 4. 启动 T1 (TCON 中的 TR1 位) \n" " 5. 如果使用中断, 需要开启串口中断控制位 ( [[51 单片机中与中断相关的寄存器#中断使能控制寄存器 IE|IE 寄存器]]) \n\n" "````ad-code\n" "title: 串口初始化, 设置串口工作方式, 根据需要的波特率设置定时器\n" "```c\n" "#define FOSC 11059200 // 单片机晶振频率\n" "#define BAUD 9600 // 波特率设置为9600\n\n" "void uart_init(void)\n" "{\n" " SCON = 0x50; // 串口工作在模式1, 8位串口, 允许接收\n" " PCON = 0x0; // 波特率不加倍\n\n" " // 通信波特率设置\n" " TMOD = 0x20; // 设置定时器1为8位自动重装模式\n" " TH1 = TL1 = 256 - FOSC / 32 / 12 / BAUD; // 定时器1赋初始值, 波特率不加倍的公式\n" " // TH1 = TL1 = 256 - 2*FOSC / 32 / 12 / BAUD; // 波特率加倍的时候\n\n" " ET1 = 0; // 禁止定时器1中断\n" " TR1 = 1; // 启动定时器1\n\n" " ES = 1; // 允许串口中断\n" " EA = 1; // 开总中断\n" "}\n" "```\n" "````\n\n" "## 串口发送数据\n\n" "````ad-code\n" "title: 串口发送一个字节数据\n" "```c\n" "// 通过串口发送1个字节出去\n" "void uart_send_byte(unsigned char c)\n" "{\n" " // 第1步, 发送一个字节\n" " SBUF = c;\n\n" " // 第2步, 先确认串口发送部分没有在忙\n" " while (!TI) ;\n\n" " // 第3步, 软件复位TI标志位\n" " TI = 0;\n" "}\n" "```\n" "````\n\n" "![[MOC-51单片机-中断#^9iots7]] \n\n" "````ad-code\n" "title: 串口发送一个字符串(多字节), 需要使用到发送单字节函数\n" "```c\n" "void uart_send_string(unsigned char *str)\n" "{\n" " while (*str != '\\0') \n" " {\n" " uart_send_byte(*str); // 发送1个字符\n" " str++; // 指针指向下一个字符\n" " }\n" "}\n" "```\n" "````\n\n" "## 串口读取数据\n\n" "![[MOC-单片机-串口通信#串口发送时的软硬件协作方式]] \n\n" "````ad-code\n" "title: 串口读取数据\n" "```cpp\n" "// brief: 通过串口读取一个字节\n" "void UART_ISR() __interrupt(4)//串口中断函数\n" "{\n" " if(RI==1)//接收中断\n" " {\n" " RI=0;//软件清零, 一定要先清零再发送数据, 让单片机串口可以继续收数据, 若是先执行下面的命令, 然后RI=0, 会丢失一些数据, 因为在发送数据器件, 串口不会接收数据\n" " uart_send_byte(SBUF) ;//把接收的数据发送到计算机\n" " }\n" "}\n" "```\n" "````\n\n" "![[MOC-51单片机-中断#^9iots7]] \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