Qt:使用正则表达式QRegularExpression匹配中文字符

您所在的位置:网站首页 正则表达式过滤汉字 Qt:使用正则表达式QRegularExpression匹配中文字符

Qt:使用正则表达式QRegularExpression匹配中文字符

2024-05-21 22:52| 来源: 网络整理| 查看: 265

QRegularExpression和QRegExp都是使用Perl风格的正则表达式语法,按理不应该有多大不同,但由于网络上的日志反复转载等,部分网络日志实际是多年前的文章,已经不足以跟上最新的技术标准。

比如:[\u4e00-\u9fa5a]  这个语句匹配,实际使用的是QRegExp。推测可能是Qt4的版本。因本人最初使用的是Qt5,故只是推测。

[\x4e00-\x9fa5a]  这个本人曾经使用过,可行,Qt5早期的版本,使用的是QRegExp。

目前的Qt5.15.2 QRegularExpression,则应该使用[\x{4e00}-\x{9fa5}]。

各个版本的语法区别基本如上。当然,Qt5.15.2 QRegularExpression还支持这样的语法 [\p{Han}]来匹配汉字字符。

更多内容请参考:Unicode Regular Expressions https://www.regular-expressions.info/unicode.html  中的 Matching a Specific Code Point说明。

实际上,Qt5.15.2的QRegularExpression类的文档中也有指出:

Different pattern syntax Porting a regular expression from QRegExp to QRegularExpression may require changes to the pattern itself. In certain scenarios, QRegExp was too lenient and accepted patterns that are simply invalid when using QRegularExpression. These are somehow easy to detect, because the QRegularExpression objects built with these patterns are not valid (cf. isValid()). In other cases, a pattern ported from QRegExp to QRegularExpression may silently change semantics. Therefore, it is necessary to review the patterns used. The most notable cases of silent incompatibility are:

Curly braces are needed in order to use a hexadecimal escape like \xHHHH with more than 2 digits. A pattern like \x2022 neeeds to be ported to \x{2022}, or it will match a space (0x20) followed by the string “22”. In general, it is highly recommended to always use curly braces with the \x escape, no matter the amount of digits specified. A 0-to-n quantification like {,n} needs to be ported to {0,n} to preserve semantics. Otherwise, a pattern such as \d{,3} would actually match a digit followed by the exact string “{,3}”. QRegExp by default does Unicode-aware matching, while QRegularExpression requires a separate option; see below for more details.

 

 

有偿解决C++编程遇到的问题,项目定制开发,联系邮箱:[email protected]。

转载请注明来源,谢谢。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3