# HG changeset patch # User František Kučera # Date 1410026168 -7200 # Node ID 06e8667694784106ccdb12e9974b60ccc054323d # Parent 547f1246fe352c58ad8638cc35a75023435b420a in-ini: regex with named groups diff -r 547f1246fe35 -r 06e866769478 java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 19:36:22 2014 +0200 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 19:56:08 2014 +0200 @@ -109,18 +109,18 @@ private enum LINE_TYPE { - COMMENT("\\s*(;|#)\\s*(.*)") { + COMMENT("\\s*(;|#)\\s*(?.*)") { @Override public void processLine(LineContext lc, FileContext fc) throws SAXException { // TODO: comment → LexicalHandler - log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2)); + log.log(Level.FINER, "Comment: {0}", lc.matcher.group("comment")); } }, - SECTION("\\s*\\[\\s*([^\\s\\]]+)\\s*\\]\\s*") { + SECTION("\\s*\\[\\s*(?[^\\s\\]]+)\\s*\\]\\s*") { @Override public void processLine(LineContext lc, FileContext fc) throws SAXException { - String name = lc.matcher.group(1); + String name = lc.matcher.group("name"); fc.outputEndSection(fc.lastSection); fc.outputStartSection(name); fc.lastSection = name; @@ -128,21 +128,25 @@ }, ENTRY( - "\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"", // quoted value → include spaces - "\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"\\s*(;|#)*\\s*(.*)", // quoted value → include spaces | with comment - "\\s*([^=\\s]+)\\s*=\\s*'([^']+)'", // apostrophed value → include spaces | with comment - "\\s*([^=\\s]+)\\s*=\\s*'([^']+)'\\s*(;|#)*\\s*(.*)", // apostrophed value → include spaces - "\\s*([^=\\s]+)\\s*=\\s*(.+)" // unquoted value → strip spaces + "\\s*(?[^=\\s]+)\\s*=\\s*\"(?[^\"]+)\"", // quoted value → include spaces + "\\s*(?[^=\\s]+)\\s*=\\s*\"(?[^\"]+)\"\\s*(;|#)*\\s*(?.*)", // quoted value → include spaces | with comment + "\\s*(?[^=\\s]+)\\s*=\\s*'(?[^']+)'", // apostrophed value → include spaces + "\\s*(?[^=\\s]+)\\s*=\\s*'(?[^']+)'\\s*((;|#)\\s*(?.*)){0,1}", // apostrophed value → include spaces | with comment + "\\s*(?[^=\\s]+)\\s*=\\s*(?.+)" // unquoted value → strip spaces ) { @Override public void processLine(LineContext lc, FileContext fc) throws SAXException { - String key = lc.matcher.group(1); - String value = lc.matcher.group(2); + String key = lc.matcher.group("key"); + String value = lc.matcher.group("value"); - if (lc.matcher.groupCount() == 4) { - String comment = lc.matcher.group(4); + try { + String comment = lc.matcher.group("comment"); // TODO: comment → LexicalHandler log.log(Level.WARNING, "Comment for entry „{0}“ is: {1}", new Object[]{key, comment}); + + } catch (IllegalArgumentException e) { + log.log(Level.WARNING, "Entry „{0}“ has no comment", key); + } fc.contentHandler.indentation(2);