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

时间:2013-08-14 08:46:16

标签: ruby regex

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

– is not the same as -; – is longer and seems to have a different character code

我没想到要测试特殊角色。

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

Testshop – Best fan ware | Example shop

应该返回

Testshop

我使用的正则表达式:

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

但是它没有返回正确的结果。问题是 - 字符。

1 个答案:

答案 0 :(得分:3)

\(短划线)外,

-是不必要的。

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

如果您只想要字母数字字符,请使用\w+也匹配_ ):

>> 'Testshop – Best fan ware | Example shop'[/\w+/]
=> "Testshop"
相关问题