I must create a regex for match the content of one file but I've one problem. In this file I control that there are, for each line, this instruction:
i=number j=number k=number u=number
But the difficulty is because I must match correctly also
j=number i=number u=number k=number
and any disposition of i,j,k and u. How can I create one regex for all the cases? Note that there can be no any ripetition of i,j,k,u on the same line. Thanks.
(([ijku]=)(?!.*\\\\2)number(?:\\\\s(?=\\\\w)|$)){4}
will work for you. This handles repetition case as well.
public static void main(String[] args){
String s = "i=number j=number k=number u=number";
System.out.println(s.matches("(([ijku]=)(?!.*\\2)number(?:\\s(?=\\w)|$)){4}"));
s = "u=number j=number k=number i=number";
System.out.println(s.matches("(([ijku]=)(?!.*\\2)number(?:\\s(?=\\w)|$)){4}"));
s = "u=number j=number k=number u=number";
System.out.println(s.matches("(([ijku]=)(?!.*\\2)number(?:\\s(?=\\w)|$)){4}"));
}
O/P :
true
true
false
您正在寻找的被称为字符类:
[ijku]=number [ijku]=number [ijku]=number [ijku]=number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.