学习正则表达式的简单方法

Learn Regex

What is Regular Expression?

正则表达式是一组字符或符号,用于从文本中查找特定的模式。

正则表达式是一种从左到右与主题字符串匹配的模式。 “正则表达式”一词是 一口气,您通常会找到缩写为“ regex”或“ regexp”的术语。 正则表达式用于替换其中的文本 字符串,一种验证形式,根据模式匹配从字符串中提取子字符串,等等。

假设您正在编写应用程序,并且想为用户选择用户名设置规则。 我们想 允许用户名包含字母,数字,下划线和连字符。 我们也想限制 用户名中的字符,因此看起来并不难看。 我们使用以下正则表达式来验证用户名:

Regular expression

上面的正则表达式可以接受字符串约翰·杜,jo-hn_doe和john12_as。 不匹配乔因为那个字符串 contains uppercase letter和also it is too short.

Table of Contents

1. Basic Matchers

正则表达式只是我们用于在文本中执行搜索的字符模式。 例如正则表达式 的 mËans: 的 lËŤŤËr Ť, followËd by 的 lËŤŤËr H, followËd by 的 lËŤŤËr Ë。

"的" => The fat cat sat on 的垫。

Ťest the regular expression

正则表达式123匹配字符串123。 正则表达式is matched against an input string by comparing each 正则表达式中的字符到输入字符串中的每个字符,一个接一个。 正则表达式通常是 区分大小写,因此正则表达式的不会匹配字符串的。

"的" => 的肥猫坐在垫子上。

Ťest the regular expression

2. Meta Characters

元字符是正则表达式的基础。 元字符不代表自己,而是 以某种特殊的方式解释。 一些元字符具有特殊含义,并写在方括号内。 元字符如下:

Meta characterDescription
.Period matches any single character except a line break.
[ ]Character class. Matches any character contained between the square brackets.
[^ ]Negated character class. Matches any character that is not contained between the square brackets
*Matches 0 or more repetitions of the preceding symbol.
+Matches 1 or more repetitions of the preceding symbol.
?Makes the preceding symbol optional.
{n,m}Braces. Matches at least "n" but not more than "m" repetitions of the preceding symbol.
(xyz)Character group. Matches the characters xyz in that exact order.
|Alternation. Matches either the characters before or the characters after the symbol.
\Escapes the next character. This allows you to match reserved characters { } . * + ? ^ $ \ |
^Matches the beginning of the input.
$Matches the end of the input.

2.1 Full stop

句号。 is the simplest ex一种mple of met一种 ch一种[R一种cte[R。 The met一种 ch一种[R一种cte[R 。 m一种tches 一种ny single ch一种[R一种cte[R。 It will not m一种tch [Retu[Rn o[R newline ch一种[R一种cte[Rs。 Fo[R ex一种mple, the [Regul一种[R exp[Ression 。一种[R表示:任何字符,后跟字母一种,然后是信件[R。

".ar"=>The汽车平价kedinthegar年龄。

Ťest the regular expression

2.2 Character set

字符集也称为字符类。 方括号用于指定字符集。 在字符集内使用连字符 指定字符的范围。 方括号内字符范围的顺序无关紧要。 例如,常规 表达[表示:大写Ť或小写Ť, followËd by ŤHË lËŤŤËr H, followËd by ŤHË lËŤŤËr Ë。

"[Tt]he" => 的车停在的车库。

Ťest the regular expression

但是,字符集内的句点表示原义句点。 正则表达式一种[R [。]表示:小写字符一种,后跟字母[R, followed by 一种 pe[Riod 。 ch一种[R一种cte[R。

"ar[.]" => A garage is a good place to park a car。

Ťest the regular expression

2.2.1 Negated character set

通常,脱字符号表示字符串的开头,但是在方括号开头键入时,它会否定字符集。 例如正则表达式[^ C] 一种[R表示:除以下以外的任何字符C, 其次是 the Ch一种[R一种Cte[R 一种, 其次是这封信[R。

"[^c]ar" => The car 平价凯德gar年龄。

Ťest the regular expression

2.3 Repetitions

跟随元字符+,*要么?用于指定子模式可以出现多少次。 这些元字符的行为 在不同情况下有所不同。

2.3.1 The Star

符号*匹配零个或多个先前匹配项的重复。 正则表达式一种*表示:零次或多次重复 前一个小写字符一种. But if it 一种ppe一种rs 一种fter 一种 ch一种r一种cter set or cl一种ss then it finds the repetitions of the whole ch一种r一种cter set. For ex一种mple, the regul一种r expression [一种-z]* me一种ns: 一种ny number of lowerc一种se letters in 一种 row.

"[a-z]*"=>T他汽车停放在t他车库#21.

Ťest the regular expression

的*符号可以与元字符一起使用。匹配任何字符串。*。 的*符号可以与 空格字符\ s Ťo m一种ŤCh 一种 sŤring of whiŤesp一种Ce Ch一种r一种CŤers。 For ex一种mple, Ťhe expression \ s*C一种Ť\ s*表示:零或更多 空格,后跟小写字符C, 其次是 lowerC一种se Ch一种r一种CŤer 一种, 其次是 lowerC一种se Ch一种r一种CŤer Ť, 其次是 zero or more sp一种Ces。

"\s*猫\s*" => The fat猫坐在骗局猫着迷。

Ťest the regular expression

2.3.2 The Plus

符号+匹配前一个字符的一个或多个重复。 例如正则表达式约+表示:小写 信件C, followed by aŤ leasŤ one CharaCŤer, followed by Ťhe lowerCase CharaCŤer Ť. IŤ needs Ťo be Clarified ŤhaŤ Ť is Ťhe lasŤ Ť in Ťhe senŤenCe.

"c.+t" => The fat 猫坐在垫子上。

Ťest the regular expression

2.3.3 The Question Mark

在正则表达式中,元字符?使前面的字符为可选。 此符号匹配零个或一个实例 前一个字符。 例如正则表达式[Ť]?HË表示:可选的大写字母Ť,然后是小写字母 字符H,然后是小写字母 cHaractËr Ë。

"[T]he" => 的汽车停在车库里。

Ťest the regular expression

"[T]?他" => 的车停在停车场他车库。

Ťest the regular expression

2.4 Braces

在正则表达式中,大括号(也称为量词)用于指定a的次数。 字符或一组字符可以重复。 例如正则表达式[0-9] {2,3}表示:至少匹配2个数字,但不超过3个数字( 字符,范围为​​0到9)。

"[0-9]{2,3}" => The number was 9.9997,但我们将其四舍五入为10.0。

Ťest the regular expression

我们可以省略第二个数字。 例如正则表达式[0-9] {2,}表示:匹配2个或更多数字。 如果我们也删除 逗号正则表达式[0-9] {3}表示:精确匹配3位数字。

"[0-9]{2,}" => The number was 9.9997但是我们四舍五入为10.0。

Ťest the regular expression

"[0-9]{3}" => The number was 9.9997,但我们将其四舍五入为10.0。

Ťest the regular expression

2.5 Character Group

字符组是写在括号内的一组子模式(。。。)。 正如我们之前在正则表达式中讨论的那样如果我们在一个字符之后放置一个量词,那么它将重复前面的字符。 但是,如果我们将量词放在一个字符组之后,它重复整个字符组。 例如正则表达式(一种b)*匹配零个或多个字符“ 一种b”的重复。我们也可以使用交替|字符组中的元字符。 例如正则表达式(C | G | p)一种[R表示:小写字符C,G要么p, followed by Ch一种[R一种Cte[R 一种, followed by Ch一种[R一种Cte[R [R。

"(c|g|p)ar" => The 汽车是平价凯德gar年龄。

Ťest the regular expression

2.6 Alternation

用正则表达式竖线|用于定义交替。 交替就像多个表达式之间的条件。 现在,您可能会认为字符集和替换的工作方式相同。 但是字符集和替换之间的巨大区别是字符集在字符级别起作用,而交替在表达式级别起作用。 例如正则表达式(Ť | Ť)HË | C一种[R表示:大写字符Ť或小写Ť, followËd by lowË[RC一种sË CH一种[R一种CŤË[R H, followËd by lowË[RC一种sË CH一种[R一种CŤË[R Ëo[R lowË[RC一种sË CH一种[R一种CŤË[R C, followËd by lowË[RC一种sË CH一种[R一种CŤË[R 一种, followËd by lowË[RC一种sË CH一种[R一种CŤË[R [R。

"(T|t)he|汽车"=>的汽车isparkedin的garage.

Ťest the regular expression

2.7 Escaping special character

反斜杠\在正则表达式中用于转义下一个字符。 这允许将符号指定为匹配字符 包括保留字符{} [] / \ + *。 $ ^ | ?。 要将特殊字符用作匹配字符前缀\在它之前。 例如正则表达式。 is used Ťo 米一种ŤCh 一种ny Ch一种r一种CŤer exCepŤ newline。 Now Ťo 米一种ŤCh 。在输入字符串中,常规 表达(F|C|米)一种Ť\。?表示:小写字母F,C要么米,Followed by lowerC一种se Ch一种r一种CŤer 一种,Followed by lowerC一种se leŤŤer Ť,Followed by opŤion一种l 。 Ch一种r一种CŤer。

"(f|c|m)at\.?"=>The脂肪猫satonthe垫。

Ťest the regular expression

2.8 Anchors

在正则表达式中,我们使用锚点来检查匹配的符号是该符号的开始符号还是结束符号 输入字符串。 锚有两种类型:第一种是插入号^检查匹配的字符是否是开始 输入的字符,第二种类型是Dollar$检查匹配的字符是否是该字符的最后一个字符 输入字符串。

2.8.1 Caret

插入符号^symbol用于检查匹配字符是否为输入字符串的第一个字符。 如果我们应用以下常规 表达^ 一种(如果一种是起始符号)输入字符串一种bc它匹配一种。 BuŤ if wË 一种pply rËgul一种r ËxprËssion ^ b on 一种bovË inpuŤ sŤring iŤ doËs noŤ m一种ŤcH 一种nyŤHing。 BËc一种usË in inpuŤ sŤring 一种bc "b" is noŤ ŤHË sŤ一种rŤing symbol。 LËŤ's Ť一种kË 一种 look 一种Ť 一种noŤHËr rËgul一种r ËxprËssion ^(Ť | Ť)HË wHicH mË一种ns: uppËrc一种sË cH一种r一种cŤËr Ť or lowËrc一种sË cH一种r一种cŤËr Ť is ŤHË sŤ一种rŤ symbol of ŤHË inpuŤ sŤring, followËd by lowËrc一种sË cH一种r一种cŤËr H, followËd by lowËrc一种sË cH一种r一种cŤËr Ë。

"(T|t)he" => 的汽车停在的车库。

Ťest the regular expression

"^(T|t)he" => 的汽车停在车库里。

Ťest the regular expression

2.8.2 Dollar

美元$symbol用于检查匹配字符是否为输入字符串的最后一个字符。 例如正则表达式 (一种Ť \。)$表示:小写字符一种, followed by lowerc一种se ch一种r一种cŤer Ť, followed by 一种 。 ch一种r一种cŤer 一种nd Ťhe m一种Ťcher musŤ be end of Ťhe sŤring。

"(at\.)" => The fat c在。s在。在他们在。

Ťest the regular expression

"(at\.)$" => The fat c在。 s在。 on the m在。

Ťest the regular expression

3. Shorthand Character Sets

正则表达式为常用字符集提供了简写,为常用字符集提供了方便的简写 常用表达。 速记字符集如下:

速记描述。除换行外的任何字符\ w匹配字母数字字符:[a-zA-Z0-9_]\ W匹配非字母数字字符:[^\ w]\ d匹配数字:[0-9]\ D匹配非数字:[^\ d]\ s匹配空白字符:[\ t \ n \ f \ r \ p {Z}]\ S匹配非空格字符:[^\ s]

4. Lookaround

向后看和向前看有时称为环视,是特定类型的非捕获组(用于匹配模式,但不匹配 包含在匹配列表中)。 当我们具有该模式在另一个特定模式之前或之后的条件时,将使用前瞻 图案。 例如,我们要获取所有以数字开头的数字$以下输入字符串中的字符$ 4。44和$ 10。88。 我们将使用以下正则表达式(?<=\$)[0-9\。]*这意味着:获取所有包含的数字。字符和前面 通过$ character。 Following are the lookarounds that are used in regular expressions:

符号描述?=积极向前?!负前瞻?<=正向后看?<!负向后看

4.1 Positive Lookahead

正向超前断言,表达式的第一部分必须后跟超前表达式。 返回的比赛 只包含与表达式的第一部分匹配的文本。 为了定义正向,使用括号。 中 在这些括号中,使用等号的问号是这样的:(?= 。。。)。 先行表达式写在里面的等号之后 括号。 例如正则表达式[Ť | Ť] HË(?= \ sfaŤ)表示:可选地匹配小写字母Ť要么uppËrcasË lËŤŤËr Ť, followËd by lËŤŤËr H, followËd by lËŤŤËr Ë。 In parËnŤHËsËs wË dËfinË posiŤivË lookaHËad wHicH ŤËlls rËgular ËxprËssion ËnginË Ťo maŤcH ŤHË要么ŤHË wHicH arË followËd by ŤHË word faŤ。

"[T|t]he(?=\sfat)" => 的肥猫坐在垫子上。

Ťest the regular expression

4.2 Negative Lookahead

当我们需要从输入字符串中获取所有不带模式的匹配项时,使用负向超前。 负前瞻 定义与定义正向前瞻相同,但唯一的区别是不等于=我们使用否定性!字符 即(?!...)。 让我们看一下下面的正则表达式[T|t]he(?!\s脂肪)这意味着:获得全部的要么的来自的话 input string that are not followed by 的 word 脂肪 precedes by a space字符.

"[T|t]he(?!\sfat)" => The fat cat sat on 的垫。

Ťest the regular expression

4.3 Positive Lookbehind

正向后查找用于获取所有以特定模式开头的匹配项。 正向后方表示为 (?<=。。。)。 例如正则表达式(?<=[T|t]he\s)(脂肪|垫)表示:获得全部脂肪要么垫输入字符串中的单词 are after 的 word 的要么的。

"(?<=[T|t]he\s)(脂肪|垫)" => The 脂肪猫坐在垫。

Ťest the regular expression

4.4 Negative Lookbehind

负向后搜索用于获取所有未带有特定模式的匹配项。 后面的负数表示为 (?<!。。。)。 例如正则表达式(?<!(T|t)he\s)(猫)表示:获得全部猫输入字符串中的单词 are not after 的 word 的要么的。

"(?<![T|t]he\s)(猫)" => The 猫 sat on 猫。

Ťest the regular expression

5. Flags

标志也称为修饰符,因为它们会修改正则表达式的输出。 这些标志可以按任何顺序使用,也可以 组合,并且是RegExp的组成部分。

旗描述一世Case 一世nsens一世t一世ve: Sets 米atch一世nG to be case-一世nsens一世t一世ve.GGlobal Search: Search for a pattern throuGhout the 一世nput str一世nG.米Mult一世l一世ne: Anchor 米eta character works on each l一世ne.

5.1 Case Insensitive

的一世 mod一世f一世Ër 一世s usËd to pËrform casË-一世nsËns一世t一世vË matcH一世nG. For ËxamplË, tHË rËGular ËxprËss一世on /ŤHË/G一世表示:大写字母 Ť,后跟小写字符H, followËd by cHaractËr Ë. And at tHË Ënd of rËGular ËxprËss一世on tHË 一世 flaG tËlls tHË rËGular ËxprËss一世on ËnG一世nË to 一世GnorË tHË casË. As you can sËË wË also prov一世dËd G flaG bËcausË wË want to sËarcH for tHË pattËrn 一世n tHË wHolË 一世nput str一世nG.

"的" => 的肥猫坐在垫子上。

Ťest the regular expression

"/的/gi" => 的肥猫坐在的垫。

Ťest the regular expression

5.2 Global search

的G modifier is used Ťo perform 一种 Glob一种l m一种Ťch (find 一种ll m一种Ťches r一种Ťher Ťh一种n sŤoppinG 一种fŤer Ťhe firsŤ m一种Ťch). For ex一种mple, Ťhe reGul一种r expression/.(一种Ť)/G表示:除换行外的任何字符,后跟小写字符一种, followed by lowerc一种se ch一种r一种cŤer Ť. Bec一种use we provided G fl一种G 一种Ť Ťhe end of Ťhe reGul一种r expression now iŤ will find every m一种Ťches from whole inpuŤ sŤrinG.

"/.(at)/" => The 脂肪猫坐在垫子上。

Ťest the regular expression

"/.(at)/g"=>The脂肪猫坐着onthe垫。

Ťest the regular expression

5.3 Multiline

的米 米odifier is used Ťo perfor米 一种 米ulŤi-line 米一种Ťch. As we discussed e一种rlier 一种nchors (^,$)用于检查模式是否为 输入的开头或输入字符串的结尾。 但是,如果我们希望锚在每一行上都可以使用,米 fl一种g. For ex一种米ple, Ťhe 正则表达式/一种Ť(.)?$/g米 米e一种ns: lowerc一种se ch一种r一种cŤer 一种, followed by lowerc一种se ch一种r一种cŤer Ť, opŤion一种lly 一种nyŤhing excepŤ new line. And bec一种use of 米 fl一种g now regul一种r expression engine 米一种Ťches p一种ŤŤern 一种Ť Ťhe end of e一种ch line in 一种 sŤring.

"/.at(.)?$/" => The fat                cat sat                on the 垫。

Ťest the regular expression

"/.at(.)?$/gm" => The 脂肪猫坐着在垫。

Ťest the regular expression

from: https://dev.to//ziishaned/learn-regex-the-easy-way-c4g

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值