I'm trying to find the correct regular expression for matching those 2 conditions:
- Must contain 1 or 2
*(2 maximum, but not necessarily consecutive) - Must contain at least two characters among digits and
.
For exemple, following strings must match : 1*2, 1**2, *2.*.
Following strings must NOT match : ab*, 1*, 1*2*.*.
So far, I've tried the following regex for the two "parts", but without any success:
- 1 or 2
*:\*(?![\d.]*\*[\d.]*\*[\d.]*) - At least two digits or
.:[\d.](?=[\*\d.]{2,})
I'm struggling finding the correct regular expressions.
You could assert 2 times a dot or a digit, and then match 1 or 2 times an asterix:
The pattern matches:
^Start of string(?=positive lookahead, assert what to the right is(?:[^\d.\n]*[\d.]){2}Match 2 times optional characters other than a digit, newline or dot, and then match a digit or dot)Close the lookahead(?:[^\n*]*\*){1,2}Repeat 1 or 2 times matching any character except a newline or*, and then match*[^\n*]*Match optional character except a newline or*$End of stringSee a regex demo