Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想编写一个匹配以下四个字符中的任何一个的正则表达式:/、*、-、+
但我在使用 / 和 * 时遇到了问题
如果我写/*/+-/有问题
/*/+-/
我尝试使用 Regex.escape 但它没有达到我的预期:我尝试过/#{Regex.escape('[+-/*]')}/并且/#{Regex.escape('[+-\/\*]')}/
/#{Regex.escape('[+-/*]')}/
/#{Regex.escape('[+-\/\*]')}/
使用百分比文字将有助于/. 在[]替代方案中,您不需要转义*and +,-如果将其放在开头或结尾,则不需要转义。
/
[]
*
+
-
%r{[/*+-]}