Java – retrieve words from strings
•
Java
I have this line:
(ADDRESS = (PROTOCOL = XXX)(HOST = YYY)(PORT = ZZZ))
I need to retrieve the host name, in this case YYY
I don't know if it's lowercase or uppercase, or even a mixture of them So I passed line Touppercase() switches the whole line to uppercase, then splits the line according to host = and I select characters until I get it), which indicates the end of the host name
I'm sure there's a better way to do this Any tips will be appreciated
My code:
String host = ""; line = line.toUpperCase(); int i = line.indexOf("HOST"); while(line.charAt(i)!='=') i++; while(line.charAt(i)!=')') { i++; if(line.charAt(i)!=')'&&line.charAt(i)!=' ') host = host + line.charAt(i); } return host;
Solution
attempt
String s= "(ADDRESS = (PROTOCOL = XXX)(HOST = YYY)(PORT = ZZZ)) }"; String host = s.replaceAll("^.+\\((HOST|host) *= *(.+?)\\).+$","$2");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码