DEV Community

Cover image for ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐˜‚๐˜€๐—ฒ ๐—ฅ๐—ฒ๐—ด๐—˜๐˜… ๐—ถ๐—ป ๐——๐—ฎ๐—ฟ๐˜?
Amr Azzam
Amr Azzam

Posted on

4

๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐˜‚๐˜€๐—ฒ ๐—ฅ๐—ฒ๐—ด๐—˜๐˜… ๐—ถ๐—ป ๐——๐—ฎ๐—ฟ๐˜?

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.

๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ ๐˜ฎ๐˜บ๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ = ๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ(๐˜ณ'๐˜ฑ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜ฏ');

Enter fullscreen mode Exit fullscreen mode

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.

๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ ๐˜ฎ๐˜บ๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ = "๐˜ฆ๐˜น๐˜ข๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ ๐˜ต๐˜ฆ๐˜น๐˜ต";
๐˜ช๐˜ง (๐˜ฎ๐˜บ๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ.๐˜ฉ๐˜ข๐˜ด๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ(๐˜ฎ๐˜บ๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ)) { 
๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ ๐˜ง๐˜ฐ๐˜ถ๐˜ฏ๐˜ฅ!");
} ๐˜ฆ๐˜ญ๐˜ด๐˜ฆ {
๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜•๐˜ฐ ๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ ๐˜ง๐˜ฐ๐˜ถ๐˜ฏ๐˜ฅ.");
}
Enter fullscreen mode Exit fullscreen mode

3- Finding matches:
Use the allMatches method to find all matches in a given string.

๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ ๐˜ฎ๐˜บ๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ = "๐˜ฆ๐˜น๐˜ข๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ ๐˜ต๐˜ฆ๐˜น๐˜ต ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ฑ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜ฏ ๐˜ฑ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜ฏ";
๐˜“๐˜ช๐˜ด๐˜ต<๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ> ๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ๐˜ฆ๐˜ด = ๐˜ฎ๐˜บ๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ.๐˜ข๐˜ญ๐˜ญ๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ๐˜ฆ๐˜ด(๐˜ฎ๐˜บ๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ);
๐˜ง๐˜ฐ๐˜ณ (๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ ๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ ๐˜ช๐˜ฏ ๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ๐˜ฆ๐˜ด) { 
๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ ๐˜ง๐˜ฐ๐˜ถ๐˜ฏ๐˜ฅ ๐˜ข๐˜ต ${๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ.๐˜ด๐˜ต๐˜ข๐˜ณ๐˜ต} - ${๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ.๐˜ฆ๐˜ฏ๐˜ฅ}: ${๐˜ฎ๐˜ข๐˜ต๐˜ค๐˜ฉ.๐˜จ๐˜ณ๐˜ฐ๐˜ถ๐˜ฑ(0)}");
}
Enter fullscreen mode Exit fullscreen mode

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);
 ๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜‹๐˜ข๐˜ต๐˜ฆ: $๐˜ฎ๐˜ฐ๐˜ฏ๐˜ต๐˜ฉ/$๐˜ฅ๐˜ข๐˜บ/$๐˜บ๐˜ฆ๐˜ข๐˜ณ");
}
Enter fullscreen mode Exit fullscreen mode

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}$');
๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ ๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ๐˜ˆ๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด = "๐˜ฆ๐˜น๐˜ข๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ@๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ.๐˜ค๐˜ฐ๐˜ฎ";
๐˜ช๐˜ง (๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ๐˜™๐˜ฆ๐˜จ๐˜Œ๐˜น๐˜ฑ.๐˜ฉ๐˜ข๐˜ด๐˜”๐˜ข๐˜ต๐˜ค๐˜ฉ(๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ๐˜ˆ๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด)) {
๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜๐˜ข๐˜ญ๐˜ช๐˜ฅ ๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ ๐˜ข๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด");
} ๐˜ฆ๐˜ญ๐˜ด๐˜ฆ {
 ๐˜ฑ๐˜ณ๐˜ช๐˜ฏ๐˜ต("๐˜๐˜ฏ๐˜ท๐˜ข๐˜ญ๐˜ช๐˜ฅ ๐˜ฆ๐˜ฎ๐˜ข๐˜ช๐˜ญ ๐˜ข๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด");
}
Enter fullscreen mode Exit fullscreen mode

๐™’๐™–๐™ž๐™ฉ ๐™›๐™ค๐™ง ๐™ฉ๐™๐™š ๐™‹๐™–๐™ฉ๐™ฉ๐™š๐™ง๐™ฃ๐™จ ๐™ค๐™› ๐™๐™š๐™œ๐™€๐™ญ ๐™ฅ๐™ค๐™จ๐™ฉ ๐Ÿš€

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more