0

Is there a way to capture the regex of group as a logic not the value captured by back reference.

For Example: The following regex would capture the IP address where the IP = A.B.C.D where A,B,C,D values ranges between 0 and 255, but A, B, C ,D shouldn't be equal. If I used back referencing in the following code:

Pattern ptr=  Pattern.compile(
"^(?<num>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
[.]\\k<num>[.]\\k<num>[.]\\k<num>$");

Then, D SHOULD EQUAL C SHOULD EQUAL B SHOULD EQUAL A.

I need to match the regex group not the value again. Do I have to type the same group every time I want to match like the following or is there a simplified way.

Pattern ptr=  Pattern.compile(
"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
[.]([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
[.]([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
[.]([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
);

0 Answers0