JAVA正则表达式获取大括号什么内容?
代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) throws Exception {
String test = "{databaseIp}:{databasePort}{instanceName};database";
List<String> ls=new ArrayList<String>();
Pattern pattern = Pattern.compile("(?<=\\{)(.+?)(?=\\})");
Matcher matcher = pattern.matcher(test);
while(matcher.find()){
ls.add(matcher.group());
}
for (String string : ls) {
System.out.println(string);
}
}
正则表达式语法实例:
public static void main(String[] args) {
// 要验证的字符串
String str = "service@xsoftlab.net";
// 邮箱验证规则
String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
}