以简单的方式包含重音字符的好正则表达式是什么? [英] What's a good regex to include accented characters in a simple way?

查看:26
本文介绍了以简单的方式包含重音字符的好正则表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的正则表达式是这样的:

Right now my regex is something like this:

[a-zA-Z0-9] 但它不像我想要的那样包含重音字符.我也想包含 - ' .

[a-zA-Z0-9] but it does not include accented characters like I would want to. I would also like - ' , to be included.

推荐答案

重音字符:DIY字符范围减法

如果您的正则表达式引擎允许(并且许多引擎允许),这将起作用:

If your regex engine allows it (and many will), this will work:

(?i)^(?:(?![×Þß÷þø])[-'0-9a-zÀ-ÿ])+$

请参阅演示(您可以添加字符进行测试).

Please see the demo (you can add characters to test).

说明

  • (?i) 设置不区分大小写模式
  • ^ 锚断言我们在字符串的开头
  • (?:(?![×Þß÷þø])[-'0-9a-zÀ-ÿ]) 匹配一个字符...
  • 前瞻(?![×Þß÷þø]) 断言字符不是括号中的字符之一
  • [-'0-9a-zÀ-ÿ] 允许在宽重音范围内使用破折号、撇号、数字、字母和字符,我们需要从中减去
  • + 匹配一次或多次
  • $ 锚断言我们在字符串的末尾
  • (?i) sets case-insensitive mode
  • The ^ anchor asserts that we are at the beginning of the string
  • (?:(?![×Þß÷þø])[-'0-9a-zÀ-ÿ]) matches one character...
  • The lookahead (?![×Þß÷þø]) asserts that the char is not one of those in the brackets
  • [-'0-9a-zÀ-ÿ] allows dash, apostrophe, digits, letters, and chars in a wide accented range, from which we need to subtract
  • The + matches that one or more times
  • The $ anchor asserts that we are at the end of the string

参考

扩展 ASCII 表

这篇关于以简单的方式包含重音字符的好正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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