<返主目录 ▕▏ C#基础库 WjwLibs: V5.0.5▕▏帮助文档 : WjwPhp正则表达式部分 ▕▏ 编译日期: 2024-09-17▕▏ Email: wujwmail@163.com
get_flags
▕→ 解析正则表达式选项的组合转换命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 解析正则表达式选项的组合转换 注: 本类中的正则表达式可用PHP的语法方式(双斜线包围正则表达式后跟选项组合) 例如: 判断字串str不为数字串 if(preg_match("!/^[\d]+$/",str)){...} 或者写为: if(preg_match("/^[\d]+$/!",str)){...} 参数: flags : 选项组合字串:(注: 有些组合会产生异常,具体如下) i: 指定不区分大小写的匹配 r: 指定搜索从右向左而不是从左向右进行 E: 指定启用符合 ECMAScript 的行为,仅能结合(i,m)的选项值。 s: 指定单行模式。 更改点(.)的含义,使其匹配,而不是除\n之外的所有字符的每个字符 m: 多行模式。 更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。 e: 指定唯一有效的捕获是显式命名或编号的 (?XXX…) 形式的组。这使未命名的圆括号可以充当非捕获组 c: 指定忽略语言中的区域性差异。 C: 将正则表达式编译为程序集 P: 消除模式中的非转义空白并启用由 # 标记的注释 !: 求反断言(仅在preg_match与???_match中有效) 返回: RegexOptions枚举 定义: public static RegexOptions get_flags(string flags)
preg_match_all
▕→ 正则全局匹配命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 正则全局匹配 参数: ptn : 正则表达式(格式:/*/标志) str : 字串 php_out : 是否兼容PHP的结果输出(false: 为C#偏平组输出) 返回: 二维结果数组 定义: public static string[][] preg_match_all(string ptn, string str, bool php_out = true)
preg_match
▕→ 正则匹配或断言命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 正则匹配或断言 参数: ptn : 正则表达式(格式:/*/标志),可用/*/!或!/*/标志做求反断言 str : 字串 list : 输出匹配结果(断言为null) 返回: 是否匹配 定义: public static bool preg_match(string ptn, string str, List<string> list = null)
preg_split
▕→ 正则分割字串命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 正则分割字串 参数: ptn : 正则表达式(格式:/*/标志) str : 字串 返回: 返回string[] 定义: public static string[] preg_split(string ptn, string str)
preg_replace
▕→ 正则替换命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 正则替换 参数: ptn : 正则表达式(格式:/*/标志) reStr : 替换字串 str : 源字串 返回: 结果字串 定义: public static string preg_replace(string ptn, string reStr,string str)
preg_quote
▕→ 转义正则表达式字符命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 转义正则表达式字符 参数: str : 输入字符串 delimiter : 如果指定了可选参数 delimiter,它也会被转义 正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! 〈 〉 | : - 缺省时本函数特殊字符将包含额外的字符"/" 如想不包含字符"/"请置参数 delimiter=null 返回: 返回转义后新的字符串, 定义: public static string preg_quote(string str, string delimiter = "")
arr_match
▕→ 快速捕获数组命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 快速捕获数组 (preg_match变形1) 参数: ptn : 正则表达式(格式:/*/标志),可用/*/!或!/*/标志做求反断言 str : 字串 返回: 匹配结果数组(null:不匹配) 定义: public static string[] arr_match(string ptn, string str)
str_match
▕→ 快速捕获字串命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 快速捕获字串 (preg_match变形2) 参数: ptn : 正则表达式(格式:/*/标志),可用/*/!或!/*/标志做求反断言 str : 字串 idx : 指定捕获索引:(组从1计) -1 : 获得一个查询结果串(有组捕获时返第一个组结果) 其它正数: 获得指定的组捕获; 无效索引: 返一个空字串 返回: 匹配字串/空值/(null:不匹配) 定义: public static string str_match(string ptn, string str,int idx=-1)
idx_match
▕→ 获得匹配(组)的结果位置命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 获得匹配(组)的结果位置 (preg_match变形3) 例(提取字串中的三组数字): string str = "报表日期: 2018年3月21日"; var arr = idx_match(@"([\d]+?)[\D]+([\d]+?)[\D]+([\d]+)[\D]+", str); int i = 0; foreach (var item in arr) { int s = item[0]; int w = item[1]; WjwPhp.print(" [" + i.ToString() + $"] => ({s},{w}) = " + str.Substring(s, w)); i++; } 参数: ptn : 正则表达式(格式:/*/标志) str : 源字串 返回: 位置二维数组int[2][],其中int[2]为当前组结果位置: [0]->起始位置,[1]->长度 定义: public static int[][] idx_match(string ptn, string str)
wjw_replace
▕→ 正则迭代回调替换命名空间: WjwLibs 所属类: WjwLibs.WjwPhp 说明: 正则迭代回调替换 类似于PHP: preg_replace_callback,这是个很棒的函数,可以搭建正则无法替代的算法 例: 将字符串中的数字串大于10000的替换成@@@ string sss = "dsfdf00123vvvv24356ddddddddt56457xxx2333ukuyxxx565460rereger"; print(sss); string str = wjw_replace(@"[\d]+", sss, (item,posi) => { if (int.Parse(item)>10000) return "@@@"; return item; }); print(str); 参数: ptn : 正则表达式,有组捕获时恒取第一组(以替代复杂的环视捕获语法) str : 源字串 func : 回调函数:参数为 string: 本次迭代捕获字串 int: 本次迭代捕获字串在源字串中的起点位置 返回: 替换本次迭代的字串(插值),为null时中止迭代取得前部分结果 返回: 替换结果的新字串 定义: public static string wjw_replace(string ptn, string str, Func<string,int,string> func)