JAVA正则表达式获取大括号内容
jdbc=jdbc:jtds:sqlserver://{databaseIp}:{databasePort}{instanceName};databaseName={databaseName};
user={databaseUser}
passoword={databasePassowrd}
怎么使用正则获取字符串为:{databaseIp}、{databasePort}、{instanceName}、{databaseName}、{databaseUser}、{databasePassowrd}
求大神解答 展开
代码如下:
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);
}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);
}
}
2017-04-28
String express = "(\\{\\w+\\})";
Matcher match = Pattern.compile(express).matcher(home);
while (match.find()) {
System.out.println(match.group(1));
}