Created with <3 with dartpad.dev.
Created
January 19, 2024 04:52
-
-
Save matifdeveloper/1449d2b90c22de684db1d41a38e2eac1 to your computer and use it in GitHub Desktop.
Text Inside Brackets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
String a = "Hello welcome [to] pakistan."; | |
RegExp regExp = RegExp(r'\[([^\]]*)\]'); | |
// Option 1: Declare match as nullable | |
RegExpMatch? match = regExp.firstMatch(a); | |
// Option 2: Use the null-aware operator (!) to assert non-null | |
// Match match = regExp.firstMatch(a)!; | |
if (match != null) { | |
String textInsideBrackets = match.group(1)!; // Add ! to assert non-null | |
print(textInsideBrackets); | |
} else { | |
print("No text inside brackets found."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment