正则学习

扫码查看

1.元字符

2. 简写

 3.回溯引用

  引用前一个表达式,当前一个表达式满足时

  eg :匹配所有<h*></h*>的内容

  

  文本:

<h1>正则表达式</h1>
Content is divided into two sections;
<h2>subTitle</h2>
another line

正则:<(h[1-6])>.*?<\/(\1)>    用"()" 标记要引用的正则块 即
(h[1-6]) 后边用(\1) 来引用第一个子表达式

  <h1>正则表达式</h1>
  Content is divided into two sections;
  <h2>subTitle</h2>
  another line

4.前后查找(前后限定)

?=Positive Lookahead正向前查找true
?!Negative Lookahead正向前查找false
?<=Positive Lookbehind反向查找true
?<!Negative Lookbehind反向查找false
 
     1. ?= 

  查询 以"  fat"结尾的The 或the ,但是不要fat

"(T|t)he(?=\sfat)" => The fat cat sat on the mat.

  2.?!

  查询 不是以"  fat"结尾的The 或the ,但是不要fat

"(T|t)he(?!\sfat)" => The fat cat sat on the mat.
  3.?!

  查询fat 或mat  但是前边必须有 the 或者 The

"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.
  4.?!

  匹配 cat,且其前不跟着 The 或 the

"(?<!(T|t)he\s)(cat)" =>  The cat sat on cat.

参考:https://github.com/ziishaned/learn-regex/blob/master/translations/README-cn.md

01-02 07:29
查看更多