don't use + ? * to search note
This commit is contained in:
xingxing
2018-01-27 12:39:23 +08:00
parent 7903d839f6
commit 6fc43424a2
2 changed files with 19 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.service.NoteFileService;
import org.houxg.leamonax.utils.FileUtils;
import org.houxg.leamonax.utils.HtmlUtils;
import org.houxg.leamonax.utils.StringUtils;
import org.houxg.leamonax.utils.TimeUtils;
import org.houxg.leamonax.widget.NoteList;
@@ -66,7 +67,7 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
if (TextUtils.isEmpty(titleKeyWord)) {
mTitleHighlight = null;
} else {
mTitleHighlight = Pattern.compile(titleKeyWord, Pattern.CASE_INSENSITIVE);
mTitleHighlight = Pattern.compile(StringUtils.escapeRegex(titleKeyWord), Pattern.CASE_INSENSITIVE);
}
}

View File

@@ -54,4 +54,21 @@ public class StringUtils {
public interface Replacer {
String replaceWith(String original, Object... extraData);
}
//转义正则表达式中的几个特殊符号
public static String escapeRegex(String str) {
int length = str.length();
StringBuilder sbf = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (c == '*') {
sbf.append('\\').append(c);
} else if (c == '+') {
sbf.append('\\').append(c);
} else if (c == '?') {
sbf.append('\\').append(c);
}
}
return sbf.toString();
}
}