19
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dart(Flutter)で正規表現による文字列チェック

Last updated at Posted at 2020-04-05

経緯

Flutterでテキストフィールドの入力チェック処理を作ろうと思ったが、意外と日本語の情報が少なかったのでメモ。

書き方

bool result = new RegExp(r'正規表現').hasMatch(value); // 文字列valueが正規表現と一致する場合、true

ちなみに、r'正規表現'rはタイプミスじゃなくて、エスケープ(\)なしで文字列で記号を扱いたいときに付ける接頭辞らしいです。便利。
(rじゃなくて@と書いている情報もありましたが、dart2.7では@だとコンパイルエラーになりました)

使用例

valueが全角カタカナかどうかチェックする場合

bool result = new RegExp(r'^[ァ-ンヴー]+$').hasMatch(value);

参考

https://www.woolha.com/tutorials/dart-using-regexp-examples
https://riptutorial.com/ja/dart/example/12472/%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE%E3%81%AE%E4%BD%9C%E6%88%90%E3%81%A8%E4%BD%BF%E7%94%A8
https://qiita.com/fubarworld2/items/9da655df4d6d69750c06

19
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?