球拍中的正则表达式
Regular expression in Racket
以下代码无效。它不包括输出中的后续文本:
(regexp-match #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd ")
'("this is a test ")
我要abcd也来。基本上我希望允许使用这些字符并删除所有其他字符:A-Za-z0-9_ 和 space (' ')。
regexp-match
returns the first match; regexp-match*
returns 所有匹配。
> (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 "
您可以尝试 regexp-replace*
和否定字符 class。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "this is a test | > < ? abcd " "")
=> "this is a test abcd "
顺便说一下,字符 class 中的 *
将匹配文字 *
。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "******" "")
=> "******"
以下代码无效。它不包括输出中的后续文本:
(regexp-match #rx"[*A-Za-z0-9_ ]+" "this is a test | > < ? abcd ")
'("this is a test ")
我要abcd也来。基本上我希望允许使用这些字符并删除所有其他字符:A-Za-z0-9_ 和 space (' ')。
regexp-match
returns the first match; regexp-match*
returns 所有匹配。
> (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 "
您可以尝试 regexp-replace*
和否定字符 class。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "this is a test | > < ? abcd " "")
=> "this is a test abcd "
顺便说一下,字符 class 中的 *
将匹配文字 *
。
(regexp-replace* #rx"[^*A-Za-z0-9_ ]" "******" "")
=> "******"