正则表达式

AUTHOR-INTRDUCTION

NAME:Yang YuHao
STATE:Freedom

This is my frist article on csdn.If i do not write well,please keep userstanding with me.


                                     ----   ONE DAY   ----

心小了,所有的小事就大了;心大了,所有大事就小了;看淡世事沧桑,内心安然无恙。 ----丰子恺

Learning content:Regex the easy way

Online test platform:https://regex101.com/
Language:Python

Catalogue

  1. What is Regular Expression ?
  2. Regular Expression basic syntax
  3. Regular Expression modifiers
  4. Regex metacharacters
  5. Regex operator
  6. Three patterns of regular expressions
  7. Regex example

What is Regular Expression

A regular expression is a group of characters or symbols which is used to find a specific pattern in a text.
A regular expression is a pattern that is matched against a subject string from left to right. Regular expressions are used to replace text within a string, validate forms, extract a substring from a string based on a pattern match, and so much more. The term “regular expression” is a mouthful, so you will usually find the term abbreviated to “regex” or “regexp”.

This is a simple case that show regex:
“ab” ==> a ab abc acb AB.
Note: Regular expressions are normally case-sensitive


Regular Expression basic syntax

  • normal characters
    Ordinary characters include all printable and nonprintable characters not explicitly specified as metacharacters. This includes all uppercase and lowercase letters, all numbers, all punctuation, and some other symbols.
  1. “[ abc ]” -----------------Matches “abc” in the all character string
    example:-------------“[ abc ]”==>a ab abc Acb
  2. “[ ^abc ]”-----------------Matches except the “abc” in the all character string
    example:-------------“[ ^abc ]”==>a ab abc Acb
  3. “[ a-d ]”-----------------Matches all words in an interview between a and b
    example:-------------“[a-e ]”==>yang is a b oy
  4. “[ \s ]”-----------------match all. \s is to match all whitespace, including newlines.
    example:-------------“[\s]”==>a ab abc
  5. “[ \S ]”-----------------match all. \S is not whitespace, excluding newlines.
    example:-------------“[\S]”==>a ab abc
  6. “.” -----------------Matches any single character except a newline (\n, \r), equivalent to [^\n\r].
    example:-------------“.”==> a ab abc Acb
  7. “\in”----------------- Match letters, numbers, underscores. Equivalent to [A-Za-z0-9_]
    example:-------------“[abc]”==>a_1 b
  • non-printing characters
Characterdescribe
\cx匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 ‘c’ 字符。
\f匹配一个换页符。等价于 \x0c 和 \cL。
\n匹配一个换行符。等价于 \x0a 和 \cJ。
\r匹配一个回车符。等价于 \x0d 和 \cM。
\s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。注意 Unicode 正则表达式会匹配全角空格符。
\S匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\t匹配一个制表符。等价于 \x09 和 \cI。
\v匹配一个垂直制表符。等价于 \x0b 和 \cK。
  • **Special characters **
Characterdescribe
$匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 ‘\n’ 或 ‘\r’。要匹配 $ 字符本身,请使用 $。
( )标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ( 和 )。
*匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 *。
+匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 +。
.匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 . 。
[标记一个中括号表达式的开始。要匹配 [,请使用 [。
?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 ?。
\将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n’ 匹配字符 ‘n’。‘\n’ 匹配换行符。序列 ‘\’ 匹配 “”,而 ‘(’ 则匹配 “(”。
^匹配输入字符串的开始位置,除非在方括号表达式中使用,当该符号在方括号表达式中使用时,表示不接受该方括号表达式中的字符集合。要匹配 ^ 字符本身,请使用 ^。
{标记限定符表达式的开始。要匹配 {,请使用 {。
  • qualifier
Characterdescribe
*匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。
+匹配前面的子表达式一次或多次。例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。
?匹配前面的子表达式零次或一次。例如,“do(es)?” 可以匹配 “do” 、 “does” 中的 “does” 、 “doxy” 中的 “do” 。? 等价于 {0,1}。
{n}n 是一个非负整数。匹配确定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o。
{n,}n 是一个非负整数。至少匹配n 次。例如,‘o{2,}’ 不能匹配 “Bob” 中的 ‘o’,但能匹配 “foooood” 中的所有 o。‘o{1,}’ 等价于 ‘o+’。‘o{0,}’ 则等价于 ‘o*’。
{n,m}m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,“o{1,3}” 将匹配 “fooooood” 中的前三个 o。‘o{0,1}’ 等价于 ‘o?’。请注意在逗号和两个数之间不能有空格。
  • Locator
characterdescrible
^匹配输入字符串开始的位置。如果设置了 RegExp 对象的 Multiline 属性,^ 还会与 \n 或 \r 之后的位置匹配。
$匹配输入字符串结尾的位置。如果设置了 RegExp 对象的 Multiline 属性,$ 还会与 \n 或 \r 之前的位置匹配。
\b匹配一个单词边界,即字与空格间的位置。
\B非单词边界匹配。

                                     ----   TWO DAY   ----

Regular Expression modifiers

业精于勤,荒于嬉;行成于思,毁于随。 ----韩愈《进学解》

modifiersdescrible
iignore - 不区分大小写 :将匹配设置为不区分大小写,搜索时不区分大小写: A 和 a 没有区别。
gglobal - 全局匹配 :查找所有的匹配项。
mmulti line - 多行匹配:使边界字符 ^ 和 $ 匹配每一行的开头和结尾,记住是多行,而不是整个字符串的开头和结尾。
s特殊字符圆点 . 中包含换行符 \n:默认情况下的圆点 . 是匹配除换行符 \n 之外的任何字符,加上 s 修饰符之后, . 中包含换行符 \n。

example

i“/ac/i”==>abc Acacd aca
g“/ac/g”==>abc Acacd aca
m“/ac/m”==>abc Acacd aca ac \n acb
s“/ac/s”==>ab /n ac /n bc /n

                                     ----   THREE DAY   ----

Regex metacharacters


                                     ----   FOUR DAY   ----

Regex operator


                                     ----   FIVE DAY   ----

Three patterns of regular expressions


                                     ----   SIX DAY   ----

Regex example

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值