我有一个字符池,我希望使用正则表达式匹配所有字符的字符或这些字符的子集。
示例:给定字符串“ACNE”,正则表达式应该给我这些结果:
我尝试过这个解决方案/b[acne]{1,4}/b
,但它接受多次重复的单个字符。
我最多只能一次拿走每个字符?
答案 0 :(得分:8)
“痤疮”这个词的子字谜是
acne
a
c
n
e
将其编译为正则表达式:
^(?!.*a.*a)(?!.*c.*c)(?!.*n.*n)(?!.*e.*e)[acne]*$
测试:regexpal
或者,由于“痤疮”不包含任何超过一次的字母,“痤疮”一词的子字谜就是
acne
将其编译为正则表达式:
^(?!.*(.).*\1)[acne]*$
测试:regexpal
注意:“magmoid”一词的子字谜可以匹配为
^(?!.*([agoid]).*\1)(?!(.*m){3})[magoid]*$
(不要多次包含agoid
个,且不要包含m
两次以上)
答案 1 :(得分:0)
使用正则表达式在给定字符串中查找单词的字形数量的代码
为Java,DataStructure,算法和公司面试问题练习提供以下存储库。请随时为存储库做出贡献
package com.techsqually.java.library.util.regularexpression;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class anagramStrings {
public static void main(String[] args) {
int count = findAnagramsInAGivenStrings("arpan","Hi arpan Aarpn we are testing rapan rranp anagram");
System.out.println(count);
}
/**
* <p> Use to find the number of anagrams of a word in a Given String</p>
* @param : word : is the word for which you want to find the anagrams
* @param : givenString : is the string in which you want to find the anagrams of word given
* @return : total number of anagrams of the word passed
*
* all words in which each character count is same but their order can be different
* e.g arpan and rapan are anagrams
*
* @output of above given example is 3, "arpan" , "Aarpn" and rapan are anagrams of arpan
* */
public static int findAnagramsInAGivenStrings(String word, String givenString){
word = word.toLowerCase();
givenString = givenString.toLowerCase();
HashMap<String,Integer> numberOfAnnagrams = new HashMap<>();
Matcher matcher = Pattern.compile("[" + word + "]{" + word.length() + "}").matcher(givenString);
int count = 0;
while (matcher.find()){
char[] matchWordArray = matcher.group().toCharArray();
char[] givenWordArray = word.toCharArray();
Arrays.sort(matchWordArray);
Arrays.sort(givenWordArray);
if (Arrays.equals(matchWordArray,givenWordArray)) count++;
}
return count;
}
}