Skip to content

Instantly share code, notes, and snippets.

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

Text Inside Brackets

Created with <3 with dartpad.dev.

extension StringExtension on String {
String? getTextInsideBrackets() {
RegExp regExp = RegExp(r'\[([^\]]*)\]');
RegExpMatch? match = regExp.firstMatch(this);
return match?.group(1);
}
}
void main() {
String a = "hello welcome [to] pakistan.";
String? textInsideBrackets = a.getTextInsideBrackets();
if (textInsideBrackets != 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