我有一个包含多个组合的字符串,如下所示。
msqlora -sn $(PWF_pdmm8107)
msqlora -n $(PWF_pdmm8107)
msqlora $(PWF_pdmm8107)
字符串是单个的。但在运行时,可能会形成上述任何一种情况。
我想从字符串中检索$(PWF_pdmm8107)令牌。
到目前为止我做了什么。
while ( st.hasMoreTokens() )
{
if ( st.nextToken().equals( "-sn" ) )
{
pwf = st.nextToken();
}
}
请建议一种方法,以便从上面的字符串组合中检索$(PWF_pdmm8107)。
由于
答案 0 :(得分:1)
你可以这样做的一种方法是使用空格作为分隔符将split()
字符串转换为数组,然后选择最后一个元素
String input = "msqlora -sn $(PWF_pdmm8107)";
String[] tmp = input.split(" ");
String output = tmp[tmp.length - 1];
答案 1 :(得分:0)
如果允许正则表达式,请考虑答案。
"(\\$\\(.*\\))"
String str = "msqlora -sn $(PWF_pdmm8107)\n" +
" msqlora -n $(PWF_pdmm8107)\n" +
" msqlora $(PWF_pdmm8107)";
Pattern compile = Pattern.compile("(\\$\\(.*\\))");
Matcher match = compile.matcher(str);
while( match.find())
{
System.out.println(match.group());
}
输出: -
$(PWF_pdmm8107)
$(PWF_pdmm8107)
$(PWF_pdmm8107)