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