qazxswk 发表于 2024-6-22 17:25:36

怎样提取运算符号两边的数字?

例如    “500/2” 或者“100*20”或者“300-100”或者“100+50”
就是这样有运算符号的字符串,提取两边的数字,两边数字长度不定。

qazxswk 发表于 2024-6-22 19:06:22

本帖最后由 qazxswk 于 2024-6-22 19:32 编辑

我自己想到一个方案,抛砖引玉,我本想通过这个代码,引入到一些需要输入数据的插件中,如拉伸、移动、偏移。希望大家提提建议。
现在,如果输入500*2,后面结果会得到1000。

(defun c:tt(/ com n txt1 txt2 zhi)
(setq com (getstring "\n输入数据:"))
(if (or
(if (setq n (vl-string-search "+" com))
(progn
(setq txt1 (substr com 1 n))
(setq txt2 (substr com (+ n 2)))
(setq zhi (+ (atof txt1) (atof txt2)))
))
(if (setq n (vl-string-search "-" com))
(progn
(setq txt1 (substr com 1 n))
(setq txt2 (substr com (+ n 2)))
(setq zhi (- (atof txt1) (atof txt2)))
))
(if (setq n (vl-string-search "*" com))
(progn
(setq txt1 (substr com 1 n))
(setq txt2 (substr com (+ n 2)))
(setq zhi (* (atof txt1) (atof txt2)))
))
(if (setq n (vl-string-search "/" com))
(progn
(setq txt1 (substr com 1 n))
(setq txt2 (substr com (+ n 2)))
(setq zhi (/ (atof txt1) (atof txt2)))
)))
(princ (strcat "\n运算结果为:"(rtos zhi)))
(princ (strcat "\n输入数值为:"com))
)
(princ))

guosheyang 发表于 2024-6-22 20:43:34

如果是为了计算值可以这样
(arxload "geomcal")
(cal "500/2")
(cal "100*20")
(cal "300-100")
(cal "100+50")

xiang19751218 发表于 2024-6-23 22:03:14

用正则表达式
;|
功能 对字符串进行正则表达式匹配测试.
参数:
pat = 正则表达式模式 ,对应vbs正则表达式的模式(expression)。说明: \\号要用\\\\替代.
str = 字符串
key = \"i\" \"g\" \"m\" , \"i\"不区分大小写(Ignorecase),\"g\"全局匹配(Global).
         \"m\"多行模式(Multiline),以上几个关键字可以组合使用,或用 \"\".
返回: 返回匹配的字符列表,或无一匹配返回nil

命令: (xd::string:regexps "\\d+\\.?\\d*" "100*20""") =>("100" "20")
命令: (xd::string:regexps "\\d+\\.?\\d*" "100.1+20""") =>("100.1" "20")
|;
(defun XD::String:RegExpS (pat str key / end keys matches x)
(if (not *xxvbsexp)
    (setq *xxvbsexp (vlax-get-or-create-object "VBScript.RegExp"))
)
(vlax-put *xxvbsexp 'Pattern pat)
(if (not key)
    (setq key "")
)
(setq key (strcase key))
(setq keys '(("I" "IgnoreCase") ("G" "Global")
         ("M" "Multiline")
      )
)
(mapcar
    '(lambda (x)
       (if (wcmatch key (strcat "*" (car x) "*"))
         (vlax-put *xxvbsexp (read (cadr x)) 0)
         (vlax-put *xxvbsexp (read (cadr x)) -1)
       )
   )
    keys
)
(setq matches (vlax-invoke *xxvbsexp 'Execute str))
(vlax-for x matches (setq end (cons (vla-get-value x) end)))
(reverse end)
)

你有种再说一遍 发表于 2024-6-23 22:19:11

xiang19751218 发表于 2024-6-23 22:03
用正则表达式
;|
功能 对字符串进行正则表达式匹配测试.


Dijkstra算法,使用两个栈实现四则表达式的计算
https://v.douyin.com/i6F6N9TD/
页: [1]
查看完整版本: 怎样提取运算符号两边的数字?