Skip to content

Instantly share code, notes, and snippets.

@yclian
Created September 21, 2012 12:28
Show Gist options
  • Save yclian/3761203 to your computer and use it in GitHub Desktop.
Save yclian/3761203 to your computer and use it in GitHub Desktop.
Search query to SQL
import java.lang.System;
import java.util.*;
import java.util.regex.*;
import java.util.regex.Pattern;
public class SearchQueryToSQL {
public static void main(String[] args) {
String search = "LOL \\* abc* def_huaua def\\_\\% [lolipop]";
Pattern asterisk = Pattern.compile("(?<!\\\\)\\*");
search = asterisk.matcher(search).replaceAll("%").replaceAll("\\\\\\*", "*");
Pattern wildcard = Pattern.compile("[_\\[\\]]");
search = wildcard.matcher(search).replaceAll("\\\\$0");
System.out.println(search);
}
}
@yclian
Copy link
Author

yclian commented Sep 21, 2012

Output:

LOL * abc% def\_huaua def\_\%

@yclian
Copy link
Author

yclian commented Sep 21, 2012

Output:

LOL * abc% def\_huaua def\\_\% \[lolipop\]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment