如何从matcher.group()中获取字符串并将其存储在java中单独的字符串变量中?
运行此源代码时,输出为: 你的名字如何从matcher.group()中获取字符串并将其存储在java中单独的字符串变量中?,java,Java,运行此源代码时,输出为: 你的名字yourdomin.com 我想将yourdomain.com存储在字符串类型变量中,以便以后使用。我指的是MatcherEmailAddressMatcher中的组(3) 我已经试过了: String emailAdress = "yourname@yourdomin.com"; Pattern emailAddress = Pattern.compile("(.*)(@)(.*)"); Matcher matchEmailAddress = emailAdd
yourdomin.com 我想将yourdomain.com存储在字符串类型变量中,以便以后使用。我指的是MatcherEmailAddressMatcher中的组(3) 我已经试过了:
String emailAdress = "yourname@yourdomin.com";
Pattern emailAddress = Pattern.compile("(.*)(@)(.*)");
Matcher matchEmailAddress = emailAddress.matcher(emailAdress);
String secondPartOfEmail;
while(matchEmailAddress.find()){
System.out.println(matchEmailAddress.group(1));
System.out.println(matchEmailAddress.group(3));
}
但是发生了一个错误。假设您只想匹配一个电子邮件地址,您可以执行以下操作:
String secondPartOfEmail = matchEmailAddress.group(3)
发生了什么错误?
String emailAdress = "yourname@yourdomin.com";
Pattern emailAddress = Pattern.compile("(.*)(@)(.*)");
Matcher matchEmailAddress = emailAddress.matcher(emailAdress);
matchEmailAddress.find(); //find the next substring matching your pattern
String secondPartOfEmail = matchEmailAddress.group(3);