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

Substitution

Processing...

Code Generator

Generated Code

$re = '/\s?\\\\([\(\)])?(?(1)|[\[\]])\s?/m'; $str = 'BERT(Bidirectional Encoder Representations from Transformers)是一种基于Transformer架构的预训练语言模型。它的核心思想是通过双向上下文建模来获取词语的上下文表示。以下是BERT模型的基本公式描述。 ### 1. BERT的基本结构 BERT模型的输入由两个部分组成: - **Token Embeddings**(词嵌入) - **Position Embeddings**(位置嵌入) - **Segment Embeddings**(句子嵌入) 这些嵌入会结合在一起作为BERT的输入表示。 #### 输入表示: 对于给定的输入句子序列 \\( X = [x_1, x_2, ..., x_n] \\),BERT首先将每个词 \\( x_i \\) 转换为其对应的词嵌入表示 \\( \\mathbf{e}_{x_i} \\)。同时,BERT还会将每个词的位置 \\( i \\) 转换为一个位置嵌入 \\( \\mathbf{e}_{p_i} \\),以及确定该词属于哪一个句子的 segment 嵌入 \\( \\mathbf{e}_{s_i} \\)。 最终,输入的表示是这些嵌入的加和: \\[ \\mathbf{e}_i = \\mathbf{e}_{x_i} + \\mathbf{e}_{p_i} + \\mathbf{e}_{s_i} \\] 其中,\\( \\mathbf{e}_{x_i} \\) 是词嵌入,\\( \\mathbf{e}_{p_i} \\) 是位置嵌入,\\( \\mathbf{e}_{s_i} \\) 是句子嵌入。 ### 2. Transformer Encoder BERT使用的是Transformer的Encoder结构。它由多个自注意力(Self-Attention)层和前馈神经网络(Feed-Forward Neural Network)层组成。每一层的自注意力计算可以表示为: #### 自注意力机制(Self-Attention): 给定输入的表示 \\( \\mathbf{E} = [\\mathbf{e}_1, \\mathbf{e}_2, ..., \\mathbf{e}_n] \\),自注意力机制通过计算查询(Query)、键(Key)和值(Value)来生成每个词的新的表示。对于输入序列中的每个词 \\( i \\),我们首先通过线性变换得到对应的查询、键和值向量: \\[ Q_i = W_Q \\mathbf{e}_i, \\quad K_i = W_K \\mathbf{e}_i, \\quad V_i = W_V \\mathbf{e}_i \\] 其中,\\( W_Q, W_K, W_V \\) 是学习到的权重矩阵。 然后,计算查询和键的点积来得到注意力得分,并应用Softmax函数得到归一化的权重: \\[ \\alpha_{ij} = \\frac{\\exp(\\mathbf{Q}_i^T \\mathbf{K}_j)}{\\sum_{k=1}^{n} \\exp(\\mathbf{Q}_i^T \\mathbf{K}_k)} \\] 注意力输出是加权的值的总和: \\[ \\text{Attention}_i = \\sum_{j=1}^{n} \\alpha_{ij} \\mathbf{V}_j \\] 最后,通过线性变换将输出映射到输出空间: \\[ \\mathbf{z}_i = W_O \\text{Attention}_i \\] 其中,\\( W_O \\) 是学习到的权重矩阵。 ### 3. BERT的输出 BERT的输出是通过堆叠多个Encoder层得到的,每一层的输出都是对输入序列中每个位置的上下文表示。在BERT中,最后一层的输出 \\( \\mathbf{h}_i \\) 代表了每个词在上下文中的表示: \\[ \\mathbf{h}_i = \\text{TransformerEncoder}(\\mathbf{e}_i) \\] ### 4. 预训练任务 BERT的预训练有两个主要任务: - **Masked Language Modeling (MLM)**:随机遮掩输入中的一些词,并训练模型预测这些被遮掩的词。 - **Next Sentence Prediction (NSP)**:预测两个句子是否是连续的句子对。 ### 5. 公式总结 BERT的总体架构可以总结为以下几个步骤: 1. 输入的词通过嵌入(Token Embeddings)、位置嵌入(Position Embeddings)和句子嵌入(Segment Embeddings)组合成初始表示: \\[ \\mathbf{e}_i = \\mathbf{e}_{x_i} + \\mathbf{e}_{p_i} + \\mathbf{e}_{s_i} \\] 2. 输入通过多个Transformer Encoder层(包括自注意力机制和前馈网络)处理。 3. 输出序列的每个位置(即每个词)的表示 \\( \\mathbf{h}_i \\) 被用来进行下游任务,如分类、问答等。 这种双向的上下文建模使得BERT能够捕获词语的丰富语义信息,相比传统的单向语言模型,BERT在很多自然语言处理任务中表现出色。'; $subst = "${1:+\$:\n\$\$\n}"; $result = preg_replace($re, $subst, $str); echo "The result of the substitution is ".$result;

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