以下代码无效。它不包括输出中的后续文本:
(regexp-match #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd ")
'("this is a test ")
我也希望abcd来。基本上我希望允许这些字符和所有其他字符重新删除:A-Za-z0-9_和空格('')。
答案 0 :(得分:3)
regexp-match
返回第一场比赛; regexp-match*
会返回所有匹配项。
> (regexp-match #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd ")
(list "this is a test ")
> (regexp-match* #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd ")
(list "this is a test " " " " " " " " abcd ")
要删除这些字符,您可以将所有匹配项与string-append*
> (string-append* (regexp-match* #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd "))
"this is a test abcd "
答案 1 :(得分:2)
您可以使用否定的字符类尝试regexp-replace*
。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "this is a test | > < ? abcd " "")
=> "this is a test abcd "
顺便说一下,字符类中的*
将与文字*
匹配。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "******" "")
=> "******"