I'm trying to figure out if my problem is solvable using regex.
I have computer name in format computer01.domain.com.
I'd like to check if number before first dot is odd or even number.
I managed to build regex to locate first character before dot ^([^.]) (?=\.)
now I can't figure out how to check if it's odd or even.
CodePudding user response:
If you want to know wether ther is an even or odd number, you might use 2 capture groups with an alternation.
If the first capture group exists, it is an odd number, if the second one exists then it is an even number.
If you only want to capture the single digit, you can also match the following dot instead of asserting it.
^[^.] (?:([13579])|([02468]))\.
The pattern matches:
^
Start of string.[^.]
Match 1 times any char other than a dot(?:
Non capture group([13579])
Capture an odd number in group 1|
Or([02468])
Capture an even number in group 2
)
Close the non capture group\.
Match the dot