List<String> playerNames = new ArrayList<>();
String line;
// This reads a line, and stores it in a variable, so you can use
// the String you read inside the loop body.
while ((line = br.readLine()) != null) {
System.out.println(line);
// Don't need counterOfReadLines, just use playerNames.size().
playerNames.add(line);
}
输出为[]
import re
def find_words(num,string):
a = re.findall('\w{int(num),}',string)
return(a)
find_words(4, "dog, cat, baby, balloon, me")
输出是[&#39; baby&#39;,&#39;气球&#39;]
答案 0 :(得分:1)
你应该用输入变量num
替换的函数部分不会像发布一样工作,因为它只是一个字符串。你必须首先替换字符串的那一部分,如下所示:
import re
def find_words(num, string):
target = '\w{%d,}' % num # if num = 4, target becomes '\w{4,}'
return re.findall(target, string)
print find_words(4, "dog, cat, baby, balloon, me") # prints ['baby', 'balloon']
或者,您可以为同样的结果执行此操作:
target = '\w{{{},}}'.format(num)
使用format()
时,需要使用双括号,以便在字符串中包含文字括号。