我可以像这样构建字符串:
String str = "Phone number %s just texted about property %s";
String.format(str, "(714) 321-2620", "690 Warwick Avenue (679871)");
//Output: Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)
Run Code Online (Sandbox Code Playgroud)
我想要实现的是与此相反.输入将跟随字符串
电话号码(714)321-2620只是发短信给690 Warwick Avenue(679871)
我想从输入中检索" (714)321-2620 "和" 690 Warwick Avenue(679871) "
任何人都可以给指针,如何在Java或Android中实现这一点?
先感谢您.
使用正则表达式:
String input = "Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)";
Matcher m = Pattern.compile("^Phone number (.*) just texted about property (.*)$").matcher(input);
if(m.find()) {
String first = m.group(1); // (714) 321-2620
String second = m.group(2); // 690 Warwick Avenue (679871)
// use the two values
}
Run Code Online (Sandbox Code Playgroud)
完整的工作代码:
import java.util.*;
import java.lang.*;
import java.util.regex.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String input = "Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)";
Matcher m = Pattern.compile("^Phone number (.*) just texted about property (.*)$").matcher(input);
if(m.find()) {
String first = m.group(1); // (714) 321-2620
String second = m.group(2); // 690 Warwick Avenue (679871)
System.out.println(first);
System.out.println(second);
}
}
Run Code Online (Sandbox Code Playgroud)
和ideone上的链接.