下面的代码只是简单地测试Wiktor的在Java的评论:
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestIt
{
public static void main (String args[])
throws Exception
{
String test1 = "character30 wordstart";
String test2 = "wordstart 30character";
String test3 = "the number 30 is here";
String failTest = "character30character";
String regex = "\\b\\d{2}|\\d{2}\\b";
Pattern pat = Pattern.compile(regex);
Matcher match = pat.matcher(test1);
System.out.println("test1: " + match.find());
match = pat.matcher(test2);
System.out.println("test2: " + match.find());
match = pat.matcher(test3);
System.out.println("test3: " + match.find());
match = pat.matcher(failTest);
System.out.println("failTest: " + match.find());
}
}
有了结果:
test1: true
test2: true
test3: true
failTest: false
[\ b \ d {2} | \ d {2} \ b'](https://regex101.com/r/4VgGMU/1)怎么办? –
让我们知道它是否有效。像这样的问题可以以不同的方式回答。如果你至少需要在一个单词边界上使用空格,你需要使用(?<!\ S)\ d {2}(?!\ d)|(?<!\ d)\ d {2} !\ S)'。 –
另一个想法是匹配字母中的两个数字序列并捕获其他字符:\ p {L} \ d {2} \ p {L} |(?<!\ d)\ d {2}(?!\ d)' –