查找代码点是否在Elixir中为大写 [英] Find if codepoint is upper case in Elixir

查看:54
本文介绍了查找代码点是否在Elixir中为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测代码点是否在Elixir中为大写字母。我尝试检查它的值是否在 65..90 范围内,但这在非拉丁大写字母上失败。我还尝试检查

I need to detect if a codepoint is an upper case letter in Elixir. I have tried checking if it's value is in the range 65..90 but this fails on non-latin upper case letters. I have also tried checking if

String.upcase(cp)== cp

但是在非字母(即数字,标点符号)上却失败了。

however this fails on non-letters (ie numbers, punctuation).

我真的不想遍历整个unicode并创建一个大写的代码点列表,是否为此提供了内置函数?

I really don't want to go through the entirety of unicode and create a list of upper case codepoints, is there a built in function for this?

推荐答案

您可以使用 \p {Lu} Unicode字符属性正则表达式转义序列以匹配任何大写字母:

You can use the \p{Lu} Unicode character property regex escape sequence to match any uppercase letter:

iex(1)> "a" =~ ~r/^\p{Lu}$/u
false
iex(2)> "A" =~ ~r/^\p{Lu}$/u
true
iex(3)> "π" =~ ~r/^\p{Lu}$/u
false
iex(4)> "Π" =~ ~r/^\p{Lu}$/u
true
iex(5)> "!" =~ ~r/^\p{Lu}$/u
false

使确保您通过 u 标志在正则表达式中打开Unicode匹配。

Make sure you pass the u flag to turn on Unicode matching in the regex.

您可以找到有关页面上的受支持属性。在页面上搜索标题 Unicode字符属性。

You can find more information about the supported properties on this page. Search for the heading "Unicode character properties" on the page.

这篇关于查找代码点是否在Elixir中为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆