Bugfix for query rewrite bug
diff --git a/CHANGES b/CHANGES
index e76855a..d9bd290 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,6 @@
-0.42 2014-09-17
+0.42 2014-09-19
         - [cleanup] Extracted private classes from KorapMatch (diewald)
+	- [bugfix] Fix query rewrite in contains-queries (diewald)
 
 0.41 2014-09-17
         - [feature] getStartPos(classNr) and getEndPos(classNr)
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanWithinQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanWithinQuery.java
index d882bde..a314475 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanWithinQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanWithinQuery.java
@@ -161,6 +161,10 @@
 	);
     };
 
+    /*
+     * Rewrite query in case it includes regular expressions or wildcards
+     */
+
     @Override
     public Query rewrite (IndexReader reader) throws IOException {
 	SpanWithinQuery clone = null;
@@ -173,6 +177,14 @@
 	    clone.embedded = query;
 	};
 
+	query = (SpanQuery) wrap.rewrite(reader);
+
+	if (query != wrap) {
+	    if (clone == null)
+		clone = this.clone();
+	    clone.wrap = query;
+	};
+
 	if (clone != null)
 	    return clone;
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
index 083f8a7..ab0729f 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
@@ -242,4 +242,44 @@
 	assertEquals("[affe afffe] baum ...", kr.getMatch(0).getSnippetBrackets());
     };
 
+
+    @Test
+    public void indexRegexWithinRewrite () throws IOException {
+	KorapIndex ki = new KorapIndex();
+
+	// abcabcabac
+	FieldDocument fd = new FieldDocument();
+	fd.addTV("base",
+		 "affe afffe baum baumgarten steingarten franz hans haus efeu effe",
+		 "[(0-4)s:affe|_0#0-4|-:t$<i>10]" +
+		 "[(5-10)s:afffe|_1#5-10]" +
+		 "[(11-15)s:baum|_2#11-15]" +
+		 "[(16-26)s:baumgarten|_3#16-26]" +
+		 "[(27-38)s:steingarten|_4#27-38]" +
+		 "[(39-44)s:franz|_5#39-44]" +
+		 "[(45-49)s:hans|_6#45-49]" +
+		 "[(50-54)s:haus|_7#50-54]" +
+		 "[(55-59)s:efeu|_8#55-59]" +
+		 "[(60-64)s:effe|_9#60-64]");
+	ki.addDoc(fd);
+
+	ki.commit();
+
+	KorapQuery kq = new KorapQuery("base");
+	SpanQuery sq = kq.contains(
+				   kq.seq(
+					  kq.re("s:a.*e")
+					  ).append(
+						   kq.re("s:af*e")
+						   ),
+				   kq.seg("s:affe")).toQuery();
+	assertEquals("spanContain(spanNext(SpanMultiTermQueryWrapper(base:/s:a.*e/), SpanMultiTermQueryWrapper(base:/s:af*e/)), base:s:affe)", sq.toString());
+	KorapSearch ks = new KorapSearch(sq);
+	ks.context.left.setToken(true).setLength(1);
+	ks.context.right.setToken(true).setLength(1);
+
+	KorapResult kr = ki.search(ks);
+	assertEquals(1, kr.getTotalResults());
+	assertEquals("[affe afffe] baum ...", kr.getMatch(0).getSnippetBrackets());
+    };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
index 970b30f..2af8786 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
@@ -879,10 +879,8 @@
 
 	kr = ki.search(sq, (short) 15);
 	assertEquals("totalResults", 0, kr.totalResults());
-
     };
 
-
     //!! Offset is 1 token tooo long
 
     @Test