How to make regexp for multiple condition?
1,087
I'm not familiar with VerbalExpression
, but a RegExp
that does this is straightforward enough.
const pattern = r'^36(01|02|03)\S{12}$';
void main() {
final regex = RegExp(pattern);
print(regex.hasMatch('3601999999999999')); // true
print(regex.hasMatch('3602999999999999')); // true
print(regex.hasMatch('3603999999999999')); // true
print(regex.hasMatch('360199999999999')); // false
print(regex.hasMatch('3600999999999999')); // false
print(regex.hasMatch('36019999999999999')); // false
}
Pattern explanation:
- The
r
prefix means dart will interpret it as a raw string ("$" and "\" are not treated as special). - The
^
and$
represent the beginning and end of the string, so it will only match the whole string and cannot find matches from just part of the string. (01|02|03)
"01" or "02" or "03".|
means OR. Wrapping it in parentheses lets it know where to stop the OR.\S
matches any non-whitespace character.{12}
means the previous thing must be repeated 12 times, so\S{12}
means any 12 non-whitespace characters.
Comments
-
nhuluseda 11 months
I have regexp code like below (I'm using VerbalExpression dart plugin ), My purpose is to check that a string starts with "36", followed by "01", "02", or "03". After that can be anything as long as the whole string is 16 characters long.
var regex = VerbalExpression() ..startOfLine() ..then("36") ..then("01") ..or("02") ..anythingBut(" ") ..endOfLine(); String nik1 = "3601999999999999"; String nik2 = "3602999999999999"; String nik3 = "3603999999999999"; print('result : ${regex.hasMatch(nik1)}'); print('Hasil : ${regex.hasMatch(nik2)}'); print('Hasil : ${regex.hasMatch(nik3)}');
my code only true for nik1 and nik2, however i want true for nik3, I noticed that i can't put or() after or() for multiple check, it just give me all false result, how do i achieve that?
-
nhuluseda about 5 yearsThanks a lot Jacob for your detailed answer and corrected post, it looks like I don't need verbal expression :)