我是正则表达式的初学者。当我们使用正则表达式验证字符串时,有没有办法找到不匹配的点或char索引?我在Java中使用RegEx来验证字符串
我只需要找到第一个不匹配的指数
的更新
请考虑这样的例子。
正则表达式:^ \ d {9} [VX] $
接受字符串:547812375X
字符串错误:547A12375X
在错误的字符串中有A而不是8.我需要的是找到不匹配的索引,因为这里是4.对于RegEx,索引处的字符不匹配。
答案 0 :(得分:6)
我认为这段代码可能会做你想要的:
package so7783938;
import static org.junit.Assert.assertEquals;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class RegexFailureTest {
public static int firstFailurePoint(Pattern regex, String str) {
for (int i = 0; i <= str.length(); i++) {
Matcher m = regex.matcher(str.substring(0, i));
if (!m.matches() && !m.hitEnd()) {
return i - 1;
}
}
if (regex.matcher(str).matches()) {
return -1;
} else {
return str.length();
}
}
@Test
public void testSimple() {
Pattern abc = Pattern.compile("abc");
assertEquals(0, firstFailurePoint(abc, ""));
assertEquals(1, firstFailurePoint(abc, "a"));
assertEquals(2, firstFailurePoint(abc, "ab"));
assertEquals(-1, firstFailurePoint(abc, "abc"));
assertEquals(3, firstFailurePoint(abc, "abcd"));
assertEquals(3, firstFailurePoint(abc, "abcdefghi"));
assertEquals(1, firstFailurePoint(abc, "aaa"));
assertEquals(2, firstFailurePoint(abc, "abb"));
}
@Test
public void testAlternative() {
Pattern regex = Pattern.compile("hello|world");
assertEquals(0, firstFailurePoint(regex, "x"));
assertEquals(-1, firstFailurePoint(regex, "hello"));
assertEquals(-1, firstFailurePoint(regex, "world"));
assertEquals(3, firstFailurePoint(regex, "hel"));
assertEquals(5, firstFailurePoint(regex, "hello kitty"));
assertEquals(3, firstFailurePoint(regex, "help me"));
assertEquals(3, firstFailurePoint(regex, "worse is better"));
}
@Test
public void testExample() {
Pattern regex = Pattern.compile("^\\d{9}[VX]$");
assertEquals(-1, firstFailurePoint(regex, "547812375X"));
assertEquals(3, firstFailurePoint(regex, "547A12375X"));
}
}