我有以下程序,
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String VALID_GUID_REGEX = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}";
Pattern NOT_PREFIXED_FILES_REGEX =
Pattern.compile("(^"+VALID_GUID_REGEX+"/\\b(foo|bar)\\b.*)|^[^/]+$");
List<String> list = new ArrayList<>();
list.add("256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a");
list.add("256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a");
list.add("govcorp/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc");
list.add("156a5037-9fc1-4e60-95c3-523d5ae1c935/123a5037-9fc1-4e60-95c3-523d5ae1c935/delta/text.doc");
list.add("123a5037-9fc1-4e60-95c3-523d5ae1c935/");
String[] keys = list.stream()
.filter(k -> NOT_PREFIXED_FILES_REGEX.matcher(k).find())
.toArray(String[]::new);
System.out.println(Arrays.toString(keys));
}
}
并且代码可以正常工作,除了列表中的最后一项,我需要满足以下条件,
256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a -- Pass
256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a -- Pass
govcorp/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc -- Fail
156a5037-9fc1-4e60-95c3-523d5ae1c935/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc -- Fail
123a5037-9fc1-4e60-95c3-523d5ae1c935/ - Pass
让我们考虑第一行,
256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a
如果我输入“ 256a5037-9fc1-4e60-95c3-523d5ae1c935 /”-通过,而“ 256a5037-9fc1-4e60-95c3-523d5ae1c935 / bar /”-通过,则是从服务器获取文件路径。
让我们考虑失败案例,“ govcorp /”-失败和“ govcorp / 123a5037-9fc1-4e60-95c3-523d5ae1c935 /”-失败
如果两个GUID序列的情况下应该失败,例如
156a5037-9fc1-4e60-95c3-523d5ae1c935 / 123a5037-9fc1-4e60-95c3-523d5ae1c935 /-失败
如果只有一种GUID情况,例如“ 123e4567-e89b-12d3-a456-426655440001 /”,则通过
最佳答案
在这里,我们首先用一个简单的表达式使我们不想要的字符串失败:
^((?!\.doc).)*$
Demo 1
然后对于其余的字符串,我们将设计第二个表达式,在这种情况下,您的原始表达式可以正常工作,我们可能只想将其与捕获组包装在一起:
([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})
Demo 2
测试
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})";
final String string = "256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a\n"
+ "256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a\n"
+ "123a5037-9fc1-4e60-95c3-523d5ae1c935/";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx电路
jex.im可视化正则表达式:
Reference