Skip to content

Commit e40f7d7

Browse files
committed
Fix character class recognition for multiple []
See https://gitlab.com/oersi/oersi-etl/-/issues/11#note_458954899
1 parent 32dde6e commit e40f7d7

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

metafacture-commons/src/main/java/org/metafacture/commons/tries/SimpleRegexTrie.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
*/
2929
public class SimpleRegexTrie<P> {
3030

31+
// Non-empty character class, containing non-[] characters, e.g.
32+
// matches: `lit-[A]`, `lit-[AB]`, does not match: `a[].1`, `a[].1.b[].1`
33+
public static final String SIMPLE_CHARACTER_CLASS = ".*\\[[^\\[\\]]+\\].*";
34+
3135
private final WildcardTrie<P> trie;
32-
public static final String SIMPLE_CHARACTER_CLASS = "\\[.+\\]";
3336

3437
public SimpleRegexTrie() {
3538
trie = new WildcardTrie<P>();
@@ -43,7 +46,7 @@ public SimpleRegexTrie() {
4346
* @param value value to associate with the key pattern
4447
*/
4548
public void put(final String keys, final P value) {
46-
if (keys.matches(".*" + SIMPLE_CHARACTER_CLASS + ".*")) {
49+
if (keys.matches(SIMPLE_CHARACTER_CLASS)) {
4750
int charClassStart = keys.indexOf('[', 0);
4851
final int charClassEnd = keys.indexOf(']', 1);
4952
String begin = keys.substring(0, charClassStart);

metafacture-commons/src/test/java/org/metafacture/commons/tries/SimpleRegexTrieTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public void testWithSimpleCharacterClass() {
4141
public void testWithEmptyCharacterClass() {
4242
final SimpleRegexTrie<String> trie = new SimpleRegexTrie<String>();
4343
// Should not be treated as character class (used for JSON arrays):
44-
trie.put("a[].1", "value");
45-
assertTrue("Expecting to find: a[].1", trie.get("a[].1").size() == 1);
44+
final String key = "a[].1.b[].1";
45+
trie.put(key, "value");
46+
assertTrue("Expecting to find: " + key, trie.get(key).size() == 1);
4647
}
4748
}

0 commit comments

Comments
 (0)