Ruby Regexp 如何匹配全部英文字母或者全部中文字母

JGpirateKing · January 30, 2015 · Last by JGpirateKing replied at January 31, 2015 · 2977 hits

例如 /some_regex/.match "PureEnglish" => #<MatchData ...> /some_regex/.match "纯中文" => #<MatchData ...> /some_regex/.match "中英mix" => nil

同一个 regexp 既能匹配纯中文又能匹配纯英文,不能匹配中英文混合 不能有空格,符合等

感谢🙏

/^\p{Han}+$/ /^[a-zA-Z]+$/

#1 楼 @blacktulip 谢谢,不过我的意思是:同一个 regexp 既能匹配纯中文又能匹配纯英文

#2 楼 @JGpirateKing 大哥,两个都列出来了,中间加条竖杠不就是 OR 了

#3 楼 @blacktulip 原来可以这样 /\A\p{Han}\z|\A[a-zA-Z]\z/u

You need to Sign in before reply, if you don't have an account, please Sign up first.