Flutter & Dart: Regular Expression Examples
Example 1: Email Verification
void main() {
RegExp exp = new RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
caseSensitive: false);
// Try it
String email1 = "[email protected]";
String email2 = "test@gmail";
String email3 = "test#gmail.com";
if (exp.hasMatch(email1)) {
print("Email1 OK");
}
if (exp.hasMatch(email2)) {
print("Email2 OK");
}
if (exp.hasMatch(email3)) {
print("Email3 OK");
}
}
output:
Email1 OK
Email2 OK
Example 2: IP address verification
coding:
void main() {
RegExp ipExp = new RegExp(r"^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$", caseSensitive: false, multiLine: false);
// Try it
// Expectation: true
if(ipExp.hasMatch('192.168.1.2')){
print('192.168.1.2 is valid');
}
// Expectation: false
if(ipExp.hasMatch('192.168.111111.55555')){
print('192.168.111111.55555 is valid');
}
}
output:
192.168.1.2 is valid
Example 3: URL Validation
coding:
void main() {
RegExp urlExp = RegExp(r"(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");
String url1 = "https://www.kindacode.com/cat/mobile/flutter/"; // valid
String url2 = "https://kindacode/cat/mobile/flutter/"; // invalid
if(urlExp.hasMatch(url1)) {
print('Url1 looks good!');
}
if(urlExp.hasMatch(url2)) {
print("Url2 looks good!");
}
}
output:
Url1 looks good!
Example 4: Domain Validation
void main() {
RegExp domainExp = RegExp(r"^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$");
String domain1 = "www.kindacode.com"; // valid
String domain2 = "www.amazon.co.uk"; // valid
String domain3 = "hello world!"; // invalid
if(domainExp.hasMatch(domain1)){
print('domain1 is valid');
}
if(domainExp.hasMatch(domain2)){
print('domain2 is valid');
}
if(domainExp.hasMatch(domain3)){
print('domain3 is valid');
}
}
output:
domain1 is valid
domain2 is valid
Flutter & Dart: Conditionally remove elements from a list
To remove elements from a list based on one or more conditions, you can use the built-in removeWhere() method.
example
Suppose we have a list of products and the task is to remove all products with a price higher than 100:
// Define how a product looks like
class Product {
final double id;
final String name;
final double price;
Product(this.id, this.name, this.price);
}
void main() {
// Generate the product list
List<Product> products = [
Product(1, "Product A", 50),
Product(2, "Product B", 101),
Product(3, "Product C", 200),
Product(4, "Product D", 90),
Product(5, "Product E", 400),
Product(6, "Product F", 777)
];
// remove products whose prices are more than 100
products.removeWhere((product) => product.price > 100);
// Print the result
print("Product(s) whose prices are less than 100:");
for (var product in products) {
print(product.name);
}
}
output:
Product(s) whose prices are less than 100:
Product A
Product D
Reference : (dart.dev).
Summarize
Read More:
- Flutter & Dart every() Method Example
- Flutter & Dart Enumeration Example
- go sync.Mutex Lock Examples
- File class details (get the file name, size, path, create, etc.)
- Python: How to Create List by Comprehension (Example Codes)
- Hutool Excel Import & Export Example
- torch.max Example (How to Use)
- Base64 Image Compression Example
- Windows Core Audio APIs: How to Progress Loopback Recording and Generate WAV File
- MultipartFile Upload an Image Example
- How to Use Printf in HAL Library
- WCNSS_qcom_cfg.ini WIFI Configuration File Guide
- Android: How to Add Background Music for Activity with Service
- Opentelemetry + Jaeger Python Version Cross Service Call Example
- Adobe ColdFusion Files Read Vulnerability (CVE-2010-2861)
- Electron: How to Use BrowserWindow to Create a Window
- Open CASCADE Technology 7.7.0 released
- Jquery use queue to implement Ajax request queue Simple Example
- Docker: How to build a rabbitmq image cluster
- C#: How to Get details of the directory where the currently running program is located