无法识别Dart RegExp空白 [英] Dart RegExp white spaces is not recognized

查看:66
本文介绍了无法识别Dart RegExp空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户名实现一个正则表达式模式,以允许英文字母,阿拉伯字母,数字,破折号和空格。


如果输入字符串,以下模式始终返回不匹配的内容即使在模式中包含\s也有一个空格

 模式_usernamePattern = r'^ [a-zA-Z0-9\u0621 -\u064A\-\s] {3,30} $'; 

我还尝试用; "

编辑:事实证明flutter为 Right-左标记或左至右标记;当使用具有LTR或RTL混合语言的文本字段时。此附加标记是一个Unicode字符,已添加到文本中。由于此附加字符,上述正则表达式失败。要解决此问题,只需对这些字符执行replaceAll即可。在此处阅读更多信息: https://github.com/flutter/flutter/issues/56514

解决方案

这是一个非常讨厌的问题,值得在此处进行解答。


  ///当在RTL字段中输入LTR文本或在
中输入RTL文本// / LTR字段,[LRM](https://en.wikipedia.org/wiki/从左至右标记)或
/// [RLM](https://en.wikipedia.org/wiki/ Right-to-left_mark)字符将分别插入
///与空白字符一起。这是为了
///消除空格中的歧义方向,并确保适当的插入符号
///位置。这些字符会影响字符串的长度,在与其他
///进行字符串比较之类的操作时,可能需要解析
///。

这是很好的意图,当您使用混合LTR / RTL文本模式时可能会引起问题(因为它是情况),并且必须确保确切的字段长度,等等。


建议解决方案是删除所有左右标记

  void main(){
final String lrm ='aaaa \u {200e} bbbb';
print(‘lrm: $ lrm,长度为$ {lrm.length}’);

最终字符串lrmFree = lrm.replaceAll(RegExp(r'\u {200e}',unicode:true),’’);
print(’lrmFree: $ lrmFree,长度为$ {lrmFree.length}’);
}

相关:从右向左(RTL)颤动


I'm trying to implement a regex pattern for username that allows English letters, Arabic letters, digits, dash and space.

The following pattern always returns no match if the input string has a space even though \s is included in the pattern

Pattern _usernamePattern = r'^[a-zA-Z0-9\u0621-\u064A\-\s]{3,30}$';

I also tried replacing \s with " " and \\s but the regex always returns no matches for any input that has a space in it.

Edit: It turns out that flutter adds a unicode character for "Right-To-Left Mark" or "Left-To-Right Mark" when using a textfield with a mix of languages that go LTR or RTL. This additional mark is a unicode character that's gets added to the text. The regex above was failing because of this additional character. To resolve the issue simply do a replaceAll for these characters. Read more here: https://github.com/flutter/flutter/issues/56514.

解决方案

This is a fairly nasty problem and worth documenting in an answer here.

As documented in the source:

  /// When LTR text is entered into an RTL field, or RTL text is entered into an
  /// LTR field, [LRM](https://en.wikipedia.org/wiki/Left-to-right_mark) or
  /// [RLM](https://en.wikipedia.org/wiki/Right-to-left_mark) characters will be
  /// inserted alongside whitespace characters, respectively. This is to
  /// eliminate ambiguous directionality in whitespace and ensure proper caret
  /// placement. These characters will affect the length of the string and may
  /// need to be parsed out when doing things like string comparison with other
  /// text.

While this is well-intended it can cause problems when you work with mixed LTR/RTL text patterns (as it is the case here) and have to ensure exact field length, etc.

The suggested solution is to remove all left-right-marks:

void main() {
  final String lrm = 'aaaa \u{200e}bbbb';
  print('lrm: "$lrm" with length ${lrm.length}');
  
  final String lrmFree = lrm.replaceAll(RegExp(r'\u{200e}', unicode: true), '');
  print('lrmFree: "$lrmFree" with length ${lrmFree.length}');
}

Related: right-to-left (RTL) in flutter

这篇关于无法识别Dart RegExp空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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