简体   繁体   English

两个精确字符串之间的正则表达式模式

[英]Regex Pattern In Between Two Exact Strings

Hi I have this working code to detect a valid UUID pattern.嗨,我有这个工作代码来检测有效的 UUID 模式。

String pattern = "\\b[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(fileName);

This correctly detects strings like:这可以正确检测字符串,例如:

0101a8ef-db10-405a-a1d2-6bebdeb17625

I would like to add two exact strings on each side of this pattern like so:我想在此模式的每一侧添加两个确切的字符串,如下所示:

FOLDER/0101a8ef-db10-405a-a1d2-6bebdeb17625.txt

Here is the code I am trying, but is not working:这是我正在尝试但不工作的代码:

String pattern = "FOLDER/\\b//[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b.txt";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(uri.toString());

There is a // in the pattern that is not in the example string.模式中有一个//不在示例字符串中。 Besides that, you can omit the word boundaries in between a character a-f0-9 and a - because it is implicit.除此之外,您可以省略字符 a-f0-9 和 a -之间的单词边界,因为它是隐含的。

Note to escape the dot to match it literally, and you can add the word boundaries at the start and at the end to prevent partial matches.注意转义点以匹配它,您可以在开头和结尾添加单词边界以防止部分匹配。

You could update the pattern to您可以将模式更新为

\\bFOLDER/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\\.txt\\b

See a regex demo .查看正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM