具有多变量和字符串arrayList的Java正则表达式
我创建了这个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)
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}”))