Ruby 匹配一个特殊的utf8字符正则表达式

Ruby 匹配一个特殊的utf8字符正则表达式,ruby,regex,Ruby,Regex,如何在正则表达式中匹配特殊符号 – is not the same as -; – is longer and seems to have a different character code 我没有想到要测试这个特殊的角色 我需要检查的字符串示例: Testshop – Best fan ware | Example shop 应该回来 Testshop 我使用的正则表达式: /[^\|\-\;\–]*/ 但是,它不会返回正确的结果。问题在于–字符。\是不必要的,除了-(破折号) 如果

如何在正则表达式中匹配特殊符号

– is not the same as -; – is longer and seems to have a different character code
我没有想到要测试这个特殊的角色

我需要检查的字符串示例:

Testshop – Best fan ware | Example shop
应该回来

Testshop
我使用的正则表达式:

/[^\|\-\;\–]*/

但是,它不会返回正确的结果。问题在于–字符。

\
是不必要的,除了
-
(破折号)

如果只需要字母数字字符,请使用
\w+
(也要匹配
\u
):


\
是不必要的,除了
-
(破折号)

如果只需要字母数字字符,请使用
\w+
(也要匹配
\u
):

>> 'Testshop – Best fan ware | Example shop'[/[^|\-;–]*/]
=> "Testshop "
>> 'Testshop – Best fan ware | Example shop'[/\w+/]
=> "Testshop"