https://www.cnblogs.com/yeungchie/
rexMatchp("pattern" "targetString")
rexMatchp("^SHORT.+" "SHORT 1. net01 - net02 in BLOCK") ; 匹配以 SHORT 开头(^),后接任意字符(.)一次或多次(+)的字符串。
; => t
rexMatchp("^CONNECT.+" "SHORT 1. net01 - net02 in BLOCK")
; => nil
rexCompile("pattern")
rexCompile("^SHORT.+") ; 定义 pattern ( 模式 / 匹配关键字 ) 。
; => t
rexExecute("targetString")
rexExecute("SHORT 1. net01 - net02 in BLOCK") ; 定义目标字符串 ( 被匹配 ) 。
; => t
rexSubstitute("outputType")
rexSubstitute("$") ; $ 用来表示 pattern 匹配到的字符串。
; => "SHORT 1. net01 - net02 in BLOCK"
"\\(pattern\\)"
来对 pattern 中的子 pattern 进行分组。rexCompile("^\\(SHORT\\).+\\(net[0-9]+\\).+\\(net[0-9]+\\).+") ; 3 个分组。
; => t
rexExecute("SHORT 1. net01 - net02 in BLOCK")
; => t
rexSubstitute("\\0") ; \\0 与上面的 $ 相同。
; => "SHORT 1. net01 - net02 in BLOCK"
rexSubstitute("\\1 -> \\2 -> \\3") ; \\ 接数字用来表示第几个分组。
; => "SHORT -> net01 -> net02"
Tips ??
rexCompile
+rexExecute
不是必须的,直接使用rexMatchp
+rexSubstitute
也可以。
pcreMatchp
、 pcreSubstitute
这两个函数完成上面的匹配效果。pcreMatchp("^(SHORT).+(net[0-9]+).+(net[0-9]+).+" "SHORT 1. net01 - net02 in BLOCK")
; => t
pcreSubstitute("\\1 -> \\2 -> \\3")
; => "SHORT -> net01 -> net02"
原文:https://www.cnblogs.com/yeungchie/p/14333186.html