假设 4 个字符串,其中 2 个匹配,另一个不匹配:
String string1 = "Hello World View";
String string2 = "Hello Nether World";
String string3 = "A World You should not find";
String string4 = "Any World is good";
什么正则表达式将仅匹配 string1
或 string4
其中 World
开始的位置
6
?
最佳答案
"^.{6}World"
假设您使用 Matcher.find
,它不需要匹配整个字符串:^
表示“字符串开头”,因此这需要开始后跟任意六个字符的字符串,然后是您的模式。
关于java - 如何使用正则表达式仅匹配字符串中特定位置的模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996884/