Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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
具有多变量和字符串arrayList的Java正则表达式_Java_Regex_Arraylist_User Controls - Fatal编程技术网

具有多变量和字符串arrayList的Java正则表达式

具有多变量和字符串arrayList的Java正则表达式,java,regex,arraylist,user-controls,Java,Regex,Arraylist,User Controls,我创建了这个Java方法: public String isInTheList(List<String> listOfStrings) { /* * Iterates through the list, and if the list contains the input of the user, * it will be returned. */ for(String string : listOfStrings)

我创建了这个Java方法:

public String isInTheList(List<String> listOfStrings)
{
    /*
     * Iterates through the list, and if the list contains the input of the user,
     * it will be returned.      
     */
    for(String string : listOfStrings)
    {
        if(this.answer.matches("(?i).*" + string + ".*"))
        {
            return string;
        }
    }    
    return null;     
}
其中,第一个列表的
字符串
s
元素
和第二个列表的
元素
都是各自列表中的元素

for(int i = 0; i < firstListOfString.size(); i++)
{
     if(userInput.contains(firstListOfString.get(i) + " " + userInput.isInTheList(secondListOfString)))
     {  
         isValid = true;//condition for exit from the while block
     }
}
for(int i=0;i
如果用户输入如下所示,则它将起作用:

第一个列表的元素+“”+第二个列表的元素

但是,如果用户输入如下所示,它也会起作用:

  (elementOfThefirstList + " " + elementOfTheSecondList) 
第一个列表的元素+“”+第二个列表的元素+“”+第一个列表的另一个元素

如何修改正则表达式和方法,使两个列表中的元素只重复一次,并在它们之间留有空格


我尝试使用另一个正则表达式,我想我会使用:“{1}”。但是,我不能用变量来解决这个问题。

根据您提供的关于如何解决这个问题的信息,关于如何解决这个问题几乎没有什么可说的。我强烈建议您在继续学习之前先阅读量词教程

让我们看看一些解决方案

  • 例如,让我们看看这行:
    如果(this.answer.matches((?i)。*“+string+”*”)

    您要做的是查看
    this.answer
    是否包含
    string
    ,忽略大小写(我怀疑您是否需要最后一个*)。但是你正在使用一个贪婪的量词来比较它们。如果这个问题是由于这个比较中的输入错误而引起的,我会考虑看链接的教程,因为不情愿的量词。
  • 好吧,这不是量词的问题。另一种可能的修复方法可能是这段代码:
    for(int i=0;i

    我不知道您使用了
    userInput
    使
    contains
    方法,但我假设您使用了containment来调用
    String
    方法。如果是这样的话,这个问题可能会有一个解决方案。仅当且仅当它等于第一个列表中的一个元素和第二个字符串中的一个匹配元素时,才必须声明它是有效的
  • 我给你的最终解决方案很简单。如果列表元素中没有其他空格,则可以
    拆分空间上连接的
    字符串
    ,并检查生成的数组包含多少个元素。如果大于2,则连接无效

  • 希望这有帮助

    什么意思你不能用一个变量来做?您能否指定如何将其他字符串连接到该字符串上?谢谢您的回答!我用double for(像一个矩阵)解决了我的问题,这个条件是userInput.matches(firstListOfString().get(i)+“{1}”+secondListOfString().get(j)+“{1}”))