Created
May 5, 2018 00:14
-
-
Save Stargator/8531db3b855bbbcd092faa16c1fd4908 to your computer and use it in GitHub Desktop.
Condtional and Lookbehind in Dart
This file contains hidden or 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() { | |
// Lookbehind example | |
final String phrase = "fooback"; | |
final RegExp lookbehindPattern = new RegExp(r'(?<=foo)back'); | |
print('Does lookbehind work? ${lookbehindPattern.hasMatch(phrase)}'); | |
// Negative Lookbehind example | |
// It would match .text or text= but not Me.text or FtextABC | |
final RegExp negativeLookbehind = new RegExp(r'(?<!(Me\.)|[frFR])(text)(?!\w)'); | |
final String phrase1 = ".text"; | |
final String phrase2 = "text="; | |
final String phrase3 = "Me.text"; | |
final String phrase4 = "FtextABC"; | |
print('Negative lookbehind test 1: ${negativeLookbehind.hasMatch(phrase1)}'); | |
print('Negative lookbehind test 1: ${negativeLookbehind.hasMatch(phrase2)}'); | |
print('Negative lookbehind test 1: ${negativeLookbehind.hasMatch(phrase3)}'); | |
print('Negative lookbehind test 1: ${negativeLookbehind.hasMatch(phrase4)}'); | |
RegExp condition = new RegExp(r'(?((?<=foo)back)[0-9]|[a-z])'); | |
print('Conditional Test: ${condition.hasMatch('fooback')}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment