解锁一个 GPT 功能,写正则表达式

发现用 GPT 的一个新功能,用来写正则表达式。

写过正则表达式的人都知道,这东西不是人写的,更多的时候我们可能就直接用别人已经写好的。

下面又这样一个要求:

regex match exactly 20 alphabetical characters, including uppercase, lowercase, and characters with diacritical marks

正则表达式需要找到 20 个英文字符。

To match exactly 20 alphabetical characters, including uppercase, lowercase, and characters with diacritical marks, you can use the following regex pattern:

^[\p{L}\p{Mn}]{20}$

This pattern breaks down as follows:

  • ^ asserts the start of the string.
  • [\p{L}\p{Mn}] matches any Unicode letter (\p{L}) or nonspacing mark (\p{Mn}).
  • {20} specifies that exactly 20 characters of the defined class should be matched.
  • $ asserts the end of the string.

You can use this regex pattern to check if a string consists of exactly 20 alphabetical characters, including uppercase, lowercase, and characters with diacritical marks.

上面的正则表达式还考虑了多语言环境,如果只是为了匹配英文字符的话,就没有那么复杂了。

可以使用下面的正则表达式:

^[a-zA-Z]{20}$