Skip to content

Instantly share code, notes, and snippets.

@matifdeveloper
Created January 19, 2024 04:52
Show Gist options
  • Save matifdeveloper/1449d2b90c22de684db1d41a38e2eac1 to your computer and use it in GitHub Desktop.
Save matifdeveloper/1449d2b90c22de684db1d41a38e2eac1 to your computer and use it in GitHub Desktop.
Text Inside Brackets

Text Inside Brackets

Created with <3 with dartpad.dev.

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