1. 其实就是调用微软的 VBScript Regular Expressions 引擎。
    2. 但是那里的代码有个地方写反了。命名格式也不统一。我修改了一下。
    3. 定义了三个函数:提取、详细提取、替换。
    4. (defun regex-extract (pat str key / regex S tmp str1)
    5. ;; 提取正则表达式匹配到的内容
    6. ;; pat 正则表达式 str 字符串
    7. ;; pat \ 使用 \\
    8. ;; key "igm" i(Ignorecase)忽略大小写 g (Global)全局匹配 m (Multili) 多行模式
    9. ;; 注意:一般使用全局匹配 g
    10. ;; 可组合使用或单独使用 或置空 ""
    11. (vl-load-com)
    12. (setq regex (vlax-create-object "Vbscript.RegExp")) ;引用正则表达式控件
    13. (if (wcmatch key "*i*,*I*")
    14. (vlax-put-property regex "IgnoreCase" 1) ;忽略大小写
    15. (vlax-put-property regex "IgnoreCase" 0) ;不忽略大小写
    16. )
    17. (if (wcmatch key "*g*,*G*")
    18. (vlax-put-property regex "Global" 1) ;匹配方式,全文字匹配
    19. (vlax-put-property regex "Global" 0)
    20. )
    21. (if (wcmatch key "*m*,*M*")
    22. (vlax-put-property regex "Multiline" 1) ;多行模式
    23. (vlax-put-property regex "Multiline" 0)
    24. )
    25. (vlax-put-property regex "Pattern" pat)
    26. (setq s (vlax-invoke-method regex "Execute" str))
    27. ;;将规则运用到STR字符,得到提取出的文字内容
    28. (VLAX-FOR tmp s ;遍历集合对象
    29. (setq str1 (cons (vlax-get-property tmp "value") str1))
    30. )
    31. ;;将内容转换为LISP语言就可以直接观察了
    32. (vlax-release-object regex)
    33. (REVERSE str1)
    34. )
    35. (defun regex-search (pat str key / REGEX S tmp POS LEN string str1)
    36. ;; 提取正则表达式匹配到的内容,输出其 位置(从0开始编号),长度,值
    37. ;; pat 正则表达式 str 字符串
    38. ;; pat \ 使用 \\
    39. ;; key "igm" i(Ignorecase)忽略大小写 g (Global)全局匹配 m (Multili) 多行模式
    40. ;; 注意:一般使用全局匹配 g
    41. ;; 可组合使用或单独使用 或置空 ""
    42. (vl-load-com)
    43. (setq regex (vlax-create-object "Vbscript.RegExp"))
    44. (if (wcmatch key "*i*,*I*")
    45. (vlax-put-property regex "IgnoreCase" 1) ;忽略大小写
    46. (vlax-put-property regex "IgnoreCase" 0) ;不忽略大小写
    47. )
    48. (if (wcmatch key "*g*,*G*")
    49. (vlax-put-property regex "Global" 1) ;匹配方式,全文字匹配
    50. (vlax-put-property regex "Global" 0) ;只检查第一处出现的位置
    51. )
    52. (if (wcmatch key "*m*,*M*")
    53. (vlax-put-property regex "Multiline" 1) ;多行模式
    54. (vlax-put-property regex "Multiline" 0) ;单行模式
    55. )
    56. (vlax-put-property regex "Pattern" pat)
    57. (setq s (vlax-invoke regex 'Execute str))
    58. (vlax-for tmp s
    59. (setq pos (vlax-get-property tmp "FirstIndex")
    60. len (vlax-get-property tmp "Length")
    61. string (vlax-get-property tmp "value")
    62. )
    63. ;;; Match对象和Matches集合只能通过 RegExp对象的Execute方法来创建,该方法实际上返回了Match
    64. ;;; 对象的集合Matches。所有的Match对象属性都是只读的。每个Match对象提供了被正则表达式搜索
    65. ;;; 找到的匹配字符串的开始位置、长度,字符串本身等信息,通过Match对象的属性供用户访问。
    66. ;;; FirstIndex在搜索字符串中匹配的位置。Length匹配字符串的长度。Value找到的匹配字符串。
    67. (setq str1 (cons (list pos len string) str1))
    68. (princ (strcat "\n文字位置 = "
    69. (itoa pos)
    70. " 文字长度 = "
    71. (itoa len)
    72. " 值 = \""
    73. string
    74. "\""
    75. )
    76. )
    77. )
    78. (vlax-release-object RegEx)
    79. (reverse str1)
    80. )
    81. (defun regex-replace (pat str str1 key / regex S str2)
    82. ;; pat 正则表达式 str 字符串 str1 替换的字符串
    83. ;; pat 中 \ 使用 \\
    84. ;; key "igm" i(Ignorecase)忽略大小写 g (Global)全局匹配 m (Multili) 多行模式
    85. ;; 注意:一般使用全局匹配 g
    86. ;; 可组合使用或单独使用 或置空 ""
    87. ;; 返回替换后的字符串
    88. (vl-load-com)
    89. (setq regex (vlax-create-object "Vbscript.RegExp")) ;引用正则表达式控件
    90. (if (wcmatch key "*i*,*I*")
    91. (vlax-put-property regex "IgnoreCase" 1) ;忽略大小写
    92. (vlax-put-property regex "IgnoreCase" 0) ;不忽略大小写
    93. )
    94. (if (wcmatch key "*g*,*G*")
    95. (vlax-put-property regex "Global" 1) ;匹配方式,全文字匹配
    96. (vlax-put-property regex "Global" 0) ;只检查第一处出现的位置
    97. )
    98. (if (wcmatch key "*m*,*M*")
    99. (vlax-put-property regex "Multiline" 1) ;多行模式
    100. (vlax-put-property regex "Multiline" 0) ;单行模式
    101. )
    102. (vlax-put-property regex "Pattern" pat)
    103. (setq STR2 (vlax-invoke-method regex "Replace" STR STR1))
    104. (vlax-release-object regex)
    105. STR2
    106. )
    107. 替换的时候,用 $1 $2 $3...表达()捕获的内容。