我有类似于这个的字符串“have防御”
我想检查包含以下字符的字符串:fcb
条件是:字符串必须包含,中的所有字符,任何顺序的。
如何为这篇文章写一个正则表达式。
我试着跟随雷克斯:
.*fcb.*如果任何一个字符匹配,它将返回true。
发布于 2016-03-29 07:33:09
别用regex。只需使用String.contains依次对每个字符进行测试:
in.contains("f") && in.contains("c") && in.contains("b")发布于 2016-03-29 07:32:18
你可以拿着煤焦把它分类。之后,您可以检查它是否包含.*b.*c.*f.*。
public static boolean contains(String input) {
    char[] inputChars = input.toCharArray();
    Arrays.sort(inputChars);
    String bufferInput = String.valueOf(inputChars);
    // Since it is sorted this will check if it simply contains `b,c and f`.
    return bufferInput.matches(".*b.*c.*f.*");
}
public static void main(String[] args) {
    System.out.println(contains("abcdefgh"));
    System.out.println(contains("abdefgh"));
}产出:
true 
false发布于 2016-03-29 07:32:35
这将检查字符串中是否存在所有的字母。
public class Example {
public static void main(String args[]) {
    String stringA = "abcdefgh";
    String opPattern = "(?=[^ ]*f)(?=[^ ]*c)(?=[^ ]*b)[^ ]+";
    Pattern opPatternRegex = Pattern.compile(opPattern);
    Matcher matcher = opPatternRegex.matcher(stringA);
    System.out.println(matcher.find());
}
}https://stackoverflow.com/questions/36277961
复制相似问题