Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子字符串或精确匹配的正则表达式_Java_Regex_String - Fatal编程技术网

Java 子字符串或精确匹配的正则表达式

Java 子字符串或精确匹配的正则表达式,java,regex,string,Java,Regex,String,给定示例字符串“hello”,我需要一个表达式来验证用户输入(现有字母的任意组合;不重复使用过的字母) 在此上下文中,有效和无效输入示例如下: 有效:“你好”、“地狱”、“lol”。。。。等等 无效:“脚跟”,“厕所”。。。等等 我试过像 (.)*([hello])(.)* 及 但是,他们不会对无效的进行排序 任何帮助都将不胜感激,谢谢 注意:这不仅仅是子字符串或精确匹配,根据示例,字母组合是有效的。正则表达式不是正确的工具…它们应该用于从左到右匹配,而不是按随机顺序计算各种字符。最好使用一个

给定示例字符串“hello”,我需要一个表达式来验证用户输入(现有字母的任意组合;不重复使用过的字母)

在此上下文中,有效和无效输入示例如下:

有效:“你好”、“地狱”、“lol”。。。。等等

无效:“脚跟”,“厕所”。。。等等

我试过像

(.)*([hello])(.)*

但是,他们不会对无效的进行排序

任何帮助都将不胜感激,谢谢


注意:这不仅仅是子字符串或精确匹配,根据示例,字母组合是有效的。

正则表达式不是正确的工具…它们应该用于从左到右匹配,而不是按随机顺序计算各种字符。最好使用一个验证字符串
hello
,循环输入字符串中的每个字符,并检查该字符是否存在(如果存在,请从验证字符串中删除该字符并继续。否则,输入失败)

以下是一份:


为什么
lol
是一个有效的条目?我希望用户能够从给定的字母组合中创建任何单词明确有效和无效答案的变化,并加强语言以提供完整的问题。非常感谢Sam,我希望可能有一个简短的表达就是一切。我有一个和你的非常相似的解决方案,所以我想我还是保持原样吧。再次感谢您的输入这可能是一个表达,但它不会短。并且可能会变得非常低效,并且会让开发人员在阅读/编辑时感到困惑。我会坚持使用您正在使用的可重用函数:)谢谢Sam,will do:)
[hello]+
public static boolean testString(String testString)
{
    String allowedCharacters = "hello";

    for(int i = 0; i < testString.length(); i++) {
        int position = allowedCharacters.indexOf(testString.charAt(i));

        if(position == -1) {
            System.out.println(testString + " - fail");
            return false;
        } else {
            allowedCharacters = allowedCharacters.substring(0, position)
                              + allowedCharacters.substring(position + 1);
        }
    }


    System.out.println(testString + " - success");
    return true;
}
testString("hello"); // hello - success
testString("hell");  // hell - success
testString("lol");   // lol - success

testString("heel");  // heel - fail
testString("loo");   // loo - fail