正则学习

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.前后查找(前后限定)

SymbolDescription描述
?= 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)hes)(fat|mat)" => The fat cat sat on the mat.
  4.?<!

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

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

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

原文地址:https://www.cnblogs.com/luyang08/p/11770380.html