Find if codepoint is upper case in Elixir

蓝咒 提交于 2019-12-30 04:55:09

问题


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).

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?


回答1:


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

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

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




回答2:


I think you could use something like this:

<< *CODEPOINT* :: utf8 >> != String.downcase(<< *CODEPOINT* :: utf8 >>)

there is maybe a better way but that's the start.



来源:https://stackoverflow.com/questions/36966980/find-if-codepoint-is-upper-case-in-elixir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!