Created
September 21, 2012 12:28
-
-
Save yclian/3761203 to your computer and use it in GitHub Desktop.
Search query to SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
Output: