用于匹配字符串的正则表达式对象, 需要另外下载安装。
属性/方法 | 类型 | 描述 |
---|---|---|
Subject | String | 要匹配的文本 |
RegEx | String | 正则表达式 |
Match() | boolean | 查找第一个匹配, 返回是否找到, 同时记入 FoundMatch |
MatchAgain() | boolean | 查找其余的匹配, 返回是否找到, 同时记入 FoundMatch |
FoundMatch | boolean | 上次查找是否找到匹配 |
Replace | String | 对查找到的一个匹配进行替换, 返回该匹配被替换成了什么 结果存入 Subject, 同时对 Stop 进行调整 |
ReplaceAll() | boolean | 进行替换全部, 结果存入 Subject |
Replacement | String | 用于替换的文本, 可包含 $n 引用子串 |
MatchedExpression | String | 匹配的字符串 |
MatchedExpressionLength | Integer | 匹配的字符串长度 |
Start | Integer | 查找匹配的开始范围,Match() 后变化 |
Stop | Integer | 查找匹配的结束范围 |
SubExpressions | String[] | 匹配到的全部子串(下标0为整个匹配, 子串从1起) |
SubExpressionLengths | Integer[] | 匹配到的全部子串长度 |
SubExpressionCount | Integer | 匹配到的子串个数 |
function GetAll(reg:TPerlRegEx;Subject,regEx:String):TStrings; begin result := TStringList.Create; reg.Subject := Subject; reg.RegEx := regEx;; if reg.Match then begin while reg.FoundMatch do begin result.Add(reg.MatchedExpression); reg.MatchAgain; end; end; end; |
GetAll(r,'john marry 123 jason','\w+'); // 列出全部单词 |
function ReplaceTo(reg:TPerlRegEx;Subject,regEx,rep:String):String; begin reg.Subject := Subject; reg.RegEx := regEx;; reg.Replacement := rep; reg.ReplaceAll; result := reg.Subject; end; |
ReplaceTo(r,'john marry 1233 jason','(\w)\1','$1'); // 替换重复字母 |
function GetSubAll(reg:TPerlRegEx;Subject,regEx:String):TStrings; var i : Integer; begin result := TStringList.Create; reg.Subject := Subject; reg.RegEx := regEx; if reg.Match then begin for i:=1 to reg.SubExpressionCount do result.Add(reg.SubExpressions[i]); end; end; |
GetSubAll(r,'2008-01-1','^(\d+)-(\d+)-(\d+)$'); // 拆解年月日 |