Expressions Régulières, caractères spéciaux { ( | etc.
Salut,
Comment faire pour matcher un string dans un fichier en utilisant les expressions régulières?
Mon fichier est de la forme:
$$ 1 "string"
$$ 2 "string"
$$ 3 "string"
Actuellement, je fais ça: "\\$\\$ (\\w+) \"(\\w+)\""
Mais ça match pas quand il y a des caractères un peu spéciaux autres que les a-z A-Z 0-9 genre '{' ou '(' dans le string.
$$ 1 "{1}"
$$ 2 "("
Tous ça (et tous les autres charactères pas pris en compte dans mon regex) sont pas matchés.
Voilà un programme de test rapide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String[] args) {
String nodeLabelRegex = "\\$\\$ (\\w+) \"(\\w+)\"";
Pattern nodeLabelPattern = Pattern.compile(nodeLabelRegex);
Matcher nodeLabelMatcher = nodeLabelPattern.matcher("$$ 1 \"abcd\"");
while (nodeLabelMatcher.find()) {
System.out.println("ligne=" + nodeLabelMatcher.group(0));
System.out.println("string=" + nodeLabelMatcher.group(2));
}
}
} |
Merci