wzg356 发表于 2025-5-10 22:58:19

wcmatch子串位置长度 -20250520优化精简-结贴

本帖最后由 wzg356 于 2025-5-19 20:51 编辑

字符串处理方面重要的补充
wcmatch子字符符位置长度-见下载文件

原先在http://bbs.mjtd.com/thread-192381-1-1.html使用
正则强大,但容易忘,不好用。wcmatch通配符用的多/易掌握,分享出来给大家

wcmatch子字符串函数Nwcmatch用法
(Nwcmatchstr字符串pattern模式符flag前缀0/后缀1/全部非01)
pattern模式符参wcmatch规则定义
支持多条件,一般常用的通配模式符基本测试了
其他见源代码注释

分离/分割字符串/字母/数字/前后缀/等等Nwcmatch全部解决
上万字符检索100多毫秒
示例:
(setq str "AB红河bg3.5hk黄河35GGf56")

(if(setq lst(Nwcmatch str "[一-龥]" nil))
(mapcar '(lambda(x)(apply 'substr(cons str x)))lst)
);分离汉字

(if(setq lst(car(Nwcmatch str "@@" 0)))
(apply 'substr(cons str lst))
);=找前缀2个字母

参考上面输出下面结果
(Nwcmatch str "?河" 2);找全部'某河'
(Nwcmatch str "##" 1);=找后缀2个数字
(Nwcmatch str "~*[~a-z]*,~*[~A-Z]*" 2);=分离字母
(Nwcmatch str "~*[~0-9]*" 1);=找后缀数字



取得/打散字符串用以下这种方案快许多(下载文件已有)
;;car-str使用示例:字符串打碎
;(breakstr "aad汉2字126")
;效率较vl-string->list法几乎翻倍
(defun breakstr(str / l1 a)
      (while(/= ""(setq a(car-str str)))
                (setq
                        str(substr str(1+(strlen a)))
                        l1(cons a l1)
                )
      )(reverse l1)
)
(defun car-str(str / a)
      (setq a(substr str 1 1))
      (cond               
                ((> (ascii a)128)
                        (substr str 1 2)
                )
                ((= "\\U+"(substr str 1 3))
                        (substr str 1 7)
                )
                (a)
      )
);取得首字符








hubeiwdlue 发表于 2025-5-11 11:37:01

谢谢大佬分享

hhh454 发表于 2025-5-11 12:27:05

好的学习资料,感谢分享

czb203 发表于 2025-5-11 16:54:17

谢谢大佬分享

wzg356 发表于 2025-5-12 08:22:09

子函数matchPRE/matchSUF返回值应该均为整数....(if n n 0)

wzg356 发表于 2025-5-13 18:12:36

20250513修正1个小bug

wzg356 发表于 2025-5-19 20:52:09

20250520优化精简-结贴
页: [1]
查看完整版本: wcmatch子串位置长度 -20250520优化精简-结贴