Reputation:
I have a string with multiple combinations like below.
msqlora -sn $(PWF_pdmm8107)
msqlora -n $(PWF_pdmm8107)
msqlora $(PWF_pdmm8107)
the string is single. but at runtime, it may be formed any one of the above situation.
I want to retrieve $(PWF_pdmm8107) token from a string.
What I have done till now.
while ( st.hasMoreTokens() )
{
if ( st.nextToken().equals( "-sn" ) )
{
pwf = st.nextToken();
}
}
please suggest a way so I can retrieve $(PWF_pdmm8107) from above string combination.
Thanks
Upvotes: 1
Views: 73
Reputation: 4534
Consider the answer if regex is allowed..
"(\\$\\(.*\\))"
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());
}
Output:-
$(PWF_pdmm8107)
$(PWF_pdmm8107)
$(PWF_pdmm8107)
Upvotes: 0
Reputation: 328
One way you could do this is to split()
the string into an array using a space as a delimiter, and select the last element
String input = "msqlora -sn $(PWF_pdmm8107)";
String[] tmp = input.split(" ");
String output = tmp[tmp.length - 1];
Upvotes: 1