It's used to match patterns in strings, allowing you to perform more complex string manipulations, validations, and extractions.
Here's a brief overview:
1- Creating a RegExp object:
Use the RegExp class to create a regular expression object. In Flutter, you can use this class to define patterns that you want to match.
๐๐ฆ๐จ๐๐น๐ฑ ๐ฎ๐บ๐๐ฆ๐จ๐๐น๐ฑ = ๐๐ฆ๐จ๐๐น๐ฑ(๐ณ'๐ฑ๐ข๐ต๐ต๐ฆ๐ณ๐ฏ');
The r prefix before the string literal is used to denote a raw string.
2- Testing for a match:
Use the hasMatch method to check if a string contains a match for the regular expression.
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฎ๐บ๐๐ต๐ณ๐ช๐ฏ๐จ = "๐ฆ๐น๐ข๐ฎ๐ฑ๐ญ๐ฆ ๐ต๐ฆ๐น๐ต";
๐ช๐ง (๐ฎ๐บ๐๐ฆ๐จ๐๐น๐ฑ.๐ฉ๐ข๐ด๐๐ข๐ต๐ค๐ฉ(๐ฎ๐บ๐๐ต๐ณ๐ช๐ฏ๐จ)) {
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ข๐ต๐ค๐ฉ ๐ง๐ฐ๐ถ๐ฏ๐ฅ!");
} ๐ฆ๐ญ๐ด๐ฆ {
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ฐ ๐ฎ๐ข๐ต๐ค๐ฉ ๐ง๐ฐ๐ถ๐ฏ๐ฅ.");
}
3- Finding matches:
Use the allMatches method to find all matches in a given string.
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฎ๐บ๐๐ต๐ณ๐ช๐ฏ๐จ = "๐ฆ๐น๐ข๐ฎ๐ฑ๐ญ๐ฆ ๐ต๐ฆ๐น๐ต ๐ธ๐ช๐ต๐ฉ ๐ฑ๐ข๐ต๐ต๐ฆ๐ณ๐ฏ ๐ฑ๐ข๐ต๐ต๐ฆ๐ณ๐ฏ";
๐๐ช๐ด๐ต<๐๐ข๐ต๐ค๐ฉ> ๐ฎ๐ข๐ต๐ค๐ฉ๐ฆ๐ด = ๐ฎ๐บ๐๐ฆ๐จ๐๐น๐ฑ.๐ข๐ญ๐ญ๐๐ข๐ต๐ค๐ฉ๐ฆ๐ด(๐ฎ๐บ๐๐ต๐ณ๐ช๐ฏ๐จ);
๐ง๐ฐ๐ณ (๐๐ข๐ต๐ค๐ฉ ๐ฎ๐ข๐ต๐ค๐ฉ ๐ช๐ฏ ๐ฎ๐ข๐ต๐ค๐ฉ๐ฆ๐ด) {
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ข๐ต๐ค๐ฉ ๐ง๐ฐ๐ถ๐ฏ๐ฅ ๐ข๐ต ${๐ฎ๐ข๐ต๐ค๐ฉ.๐ด๐ต๐ข๐ณ๐ต} - ${๐ฎ๐ข๐ต๐ค๐ฉ.๐ฆ๐ฏ๐ฅ}: ${๐ฎ๐ข๐ต๐ค๐ฉ.๐จ๐ณ๐ฐ๐ถ๐ฑ(0)}");
}
4- Extracting matched groups:
If your regular expression contains groups, you can access them using the group method of the Match object.
๐๐ฆ๐จ๐๐น๐ฑ ๐ฎ๐บ๐๐ฆ๐จ๐๐น๐ฑ = ๐๐ฆ๐จ๐๐น๐ฑ(๐ณ'(\๐ฅ{2})/(\๐ฅ{2})/(\๐ฅ{4})');
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฅ๐ข๐ต๐ฆ๐๐ต๐ณ๐ช๐ฏ๐จ = "01/17/2024";
๐๐ข๐ต๐ค๐ฉ ๐ฎ๐ข๐ต๐ค๐ฉ = ๐ฎ๐บ๐๐ฆ๐จ๐๐น๐ฑ.๐ง๐ช๐ณ๐ด๐ต๐๐ข๐ต๐ค๐ฉ(๐ฅ๐ข๐ต๐ฆ๐๐ต๐ณ๐ช๐ฏ๐จ);
๐ช๐ง (๐ฎ๐ข๐ต๐ค๐ฉ != ๐ฏ๐ถ๐ญ๐ญ) {
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฅ๐ข๐บ = ๐ฎ๐ข๐ต๐ค๐ฉ.๐จ๐ณ๐ฐ๐ถ๐ฑ(1);
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฎ๐ฐ๐ฏ๐ต๐ฉ = ๐ฎ๐ข๐ต๐ค๐ฉ.๐จ๐ณ๐ฐ๐ถ๐ฑ(2);
๐๐ต๐ณ๐ช๐ฏ๐จ ๐บ๐ฆ๐ข๐ณ = ๐ฎ๐ข๐ต๐ค๐ฉ.๐จ๐ณ๐ฐ๐ถ๐ฑ(3);
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ข๐ต๐ฆ: $๐ฎ๐ฐ๐ฏ๐ต๐ฉ/$๐ฅ๐ข๐บ/$๐บ๐ฆ๐ข๐ณ");
}
5- Testing in a Flutter application:
You can use regular expressions in various scenarios in Flutter, such as form validations, parsing data, or filtering lists. For example, you might use a regular expression to validate an email address entered in a form.
๐๐ฆ๐จ๐๐น๐ฑ ๐ฆ๐ฎ๐ข๐ช๐ญ๐๐ฆ๐จ๐๐น๐ฑ = ๐๐ฆ๐จ๐๐น๐ฑ(๐ณ'^[\๐ธ-]+(\.[\๐ธ-]+)*@([\๐ธ-]+\.)+[๐ข-๐ป๐-๐ก] {2,7}$');
๐๐ต๐ณ๐ช๐ฏ๐จ ๐ฆ๐ฎ๐ข๐ช๐ญ๐๐ฅ๐ฅ๐ณ๐ฆ๐ด๐ด = "๐ฆ๐น๐ข๐ฎ๐ฑ๐ญ๐ฆ@๐ฆ๐ฎ๐ข๐ช๐ญ.๐ค๐ฐ๐ฎ";
๐ช๐ง (๐ฆ๐ฎ๐ข๐ช๐ญ๐๐ฆ๐จ๐๐น๐ฑ.๐ฉ๐ข๐ด๐๐ข๐ต๐ค๐ฉ(๐ฆ๐ฎ๐ข๐ช๐ญ๐๐ฅ๐ฅ๐ณ๐ฆ๐ด๐ด)) {
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ข๐ญ๐ช๐ฅ ๐ฆ๐ฎ๐ข๐ช๐ญ ๐ข๐ฅ๐ฅ๐ณ๐ฆ๐ด๐ด");
} ๐ฆ๐ญ๐ด๐ฆ {
๐ฑ๐ณ๐ช๐ฏ๐ต("๐๐ฏ๐ท๐ข๐ญ๐ช๐ฅ ๐ฆ๐ฎ๐ข๐ช๐ญ ๐ข๐ฅ๐ฅ๐ณ๐ฆ๐ด๐ด");
}
๐๐๐๐ฉ ๐๐ค๐ง ๐ฉ๐๐ ๐๐๐ฉ๐ฉ๐๐ง๐ฃ๐จ ๐ค๐ ๐๐๐๐๐ญ ๐ฅ๐ค๐จ๐ฉ ๐
Top comments (0)