Olá:
Criei um programa para testar o método String.matches(). Minha JDK era a 1.4.2_05 Ele ficou assim:
public class TestaRegex {
private static String[] strings = {
"0123456789",
"012345abc9",
"abcdefghij",
"jihgfedcba",
"jihgfe000a",
"abc"
};
private static String pattern = "^.{6}(abc|dcb)"; // Verifica se depois do 6o. caractere apareça "abc" ou "dcb".
public static void testarStringMatches() {
for(int i = 0; i < strings.length; i++) {
System.out.println("\"" + strings[i] + "\".matches(" + pattern + ")? " + strings[i].matches(pattern));
}
}
public static void main(String[] args) {
testarStringMatches();
}
}
Minha espectativa era que para “012345abc9” e “jihgfedcba” seria exibido true. Entretanto para todos ele exibiu false.
Tenho uma página HTML que usa o RegExp do Javascript:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Teste de Expressões Regulares
</title>
<meta http-equiv="Content-Type" content="
text/html; charset=us-ascii" />
<!-- Código retirado de http://www.regular-expressions.info/javascriptexample.html -->
<script language="JavaScript" type="text/javascript">
//<![CDATA[
<!--
function demoMatchClick() {
var re = new RegExp(document.demoMatch.regex.value);
if (document.demoMatch.subject.value.match(re)) {
alert("Successful match");
} else {
alert("No match");
}
}
// -->
//]]>
</script>
<meta content="MSHTML 6.00.2800.1476" name="GENERATOR" />
</head>
<body>
<form id="demoMatch" name="demoMatch" action="javascript:void(0)"
method="post">
<p>
Regexp: <input size="100" name="regex" />
</p><!-- VALUE="\bt[a-z]+\b" -->
<p>
Subject string: <input size="50" name="subject" />
</p><!-- VALUE="This is a test of the JavaScript RegExp object" -->
<p>
<input onclick="demoMatchClick()" type="submit" value="
Test Match" />
</form>
</body>
</html>
Este código funcionou perfeitamente com as strings do meu programa. Então aparentemente o padrão está correto. Então por que o programa não funciona como deveria?
Grato,