我有我的java项目的问题,我没有添加匹配组合,我有一个字典和混合,字典是有正常的词,在混合从字典一样;在dic。=“鼠标”mixed= "usemo“那些是同一个词,我想做的,如果我写在混合"usmo”程序去字典和4个字母是相同的在字典所以单词是鼠标,或混合= "usem“字典可以改变”鼠标“,我搜索这可以用正则表达式,但我不知道怎么做,有人能帮我吗??
发布于 2017-11-15 05:44:07
下面是运行代码,它将与您使用正则表达式搜索的模式进行匹配,并打印字典中的匹配结果。
public class RegexTestStrings {
private static HashSet<String> dictionarySet = new HashSet<String>();
private static HashSet<String> regExSetSet = new HashSet<String>();
public static void main(String[] args) {
dictionarySet.add("mouse");
dictionarySet.add("tiger");
dictionarySet.add("monkey");
RegexTestStrings regexTestStrings=new RegexTestStrings();
System.out.println(regexTestStrings.fidMatchingWord(dictionarySet,"usemo"));
System.out.println(regexTestStrings.fidMatchingWord(dictionarySet,"usem"));
System.out.println(regexTestStrings.fidMatchingWord(dictionarySet,"usmo"));
System.out.println(regexTestStrings.fidMatchingWord(dictionarySet,"kmoy"));
}
private String fidMatchingWord(HashSet<String> dictionarySet2, String searchWord) {
String result=null;
Iterator<String> dictionarIterator=dictionarySet.iterator();
while(dictionarIterator.hasNext()){
regExSetSet.clear();
String inputString=dictionarIterator.next();
findAllRegEx(inputString.toCharArray(), 0, inputString.length()-1);
Iterator<String> resultIterator=regExSetSet.iterator();
// Regular expression to match the pattern of search string
String pattern = "(?s)^(" + Pattern.quote(searchWord) + ".*$|.*" + Pattern.quote(searchWord) + ")$";
while(resultIterator.hasNext()){
String str=resultIterator.next();
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
boolean b = m.matches();
if(b){
result=inputString;
break;
}
}
}
return result;
}
public static void findAllRegEx(char[] ary, int startIndex, int endIndex) {
if(startIndex == endIndex){
regExSetSet.add(String.valueOf(ary));
}else{
for(int i=startIndex;i<=endIndex;i++) {
swap(ary, startIndex, i );
findAllRegEx(ary, startIndex+1, endIndex);
swap(ary, startIndex, i );
}
}
}
public static void swap(char[] arr, int x, int y) {
char temp = ary[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
Output
______________
mouse
mouse
mouse
monkey
https://stackoverflow.com/questions/47293472
复制相似问题