英文:
Regex to find database name from Jdbc url
问题
以下是翻译好的部分:
我已经尝试过:
Pattern pattern = Pattern.compile("^(;databaseName=)(.*?)([;])", Pattern.CASE_INSENSITIVE);
英文:
What will be the regular expression forbelow jdbc urls to find the databaseName. Output will be "ab_cd_e".
String jdbcUrl="jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;integratedSecurity=false;databaseName=ab_cd_e";
String jdbcUrl="jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false";
I have tried:
Pattern pattern = Pattern.compile("^(;databaseName=)(.*?)([;])", Pattern.CASE_INSENSITIVE);
答案1
得分: 2
你只需要提取在部分 databaseName=
和 ;
或行尾的 $
之间的字符串。为了实现这一点,您可以使用正向先行断言 (?=;|$)
和正向后行断言 (?<=databaseName=)
。
因此,正则表达式 (?<=databaseName=)\w+(?=;|$)
将给您提供 ab_cd_e
。
尝试这样做:
Pattern pattern = Pattern.compile("(?<=databaseName=)\\w+(?=;|$)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jdbcUrl);
if (matcher.find())
System.out.println(matcher.group());
英文:
You just need to extract the string between the parts databaseName=
and ;
or $
end of line. To achieve this you can use a positive lookahead (?=;|$)
and a positive lookbehind (?<=databaseName=)
Hence, the regex (?<=databaseName=)\\w+(?=;|$)
gives you ab_cd_e
Try this:
Pattern pattern = Pattern.compile("(?<=databaseName=)\\w+(?=;|$)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jdbcUrl);
if (matcher.find())
System.out.println(matcher.group());
答案2
得分: 1
使用
;databaseName=([^;]*)
见[证明][1]
**解释**
解释
--------------------------------------------------------------------------------
;databaseName= ';databaseName='
--------------------------------------------------------------------------------
( 分组并捕获到 :
--------------------------------------------------------------------------------
[^;]* 除 ';' 之外的任何字符(0 次或多次
(匹配最大数量)
--------------------------------------------------------------------------------
) 的结束
[Java 代码][2]:
import java.util.regex.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = ";databaseName=([^;]*)";
final String string = "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false\n" + "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;integratedSecurity=false;databaseName=ab_cd_e";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Name: " + matcher.group(1));
}
}
}
打印
Name: ab_cd_e
Name: ab_cd_e
[1]: https://regex101.com/r/yaU0DY/1/
[2]: http://%3Ca%20href=%22https://tio.run/##pZE/b8IwEMXn5FOcmJJWNVCxNFGmiq1ULam6UIoujgmmiZPaF/4I8dmpIQEJqZ3q5Vln/d67Oy9xhXfL9OtwkEVVaoKlLbCaZM60yMSG3YQuuDxHY@BNGHo83tyd61R1kksOhpCsrEqZQoFSgReTliqbTAF1ZnyghS7XprHNUWVsuOGiIlkq17E2zlwqzKGB4BQJEXTCFAkTNOIZCxF5k89weuN3QtcBe64Q04hllmnCA/OdG6FXQgfdLiac8bII@oNBL5TKtqp4YziK49eneDh@H47ve/2H6zRMZjydCUuQyDSSSGPBay1pG83Run@oDtz@K@4v51/7OE/djv2CREIrqFqNzpVjdiVz4Z2W6F9TIyS@ENp@UaPRmWdtxWv2eMHWC2sFXvvKrEvq@T7s2meAeGtIFKysiVWWpFx5nWPbARy3c@YyXdaV1/cvvnvX2bv7w@EH%22%20title=%22Java%20(JDK)%20%E2%80%93%20尝试在线!%3C/a%3E
英文:
Use
;databaseName=([^;]*)
See proof
Explanation
EXPLANATION
--------------------------------------------------------------------------------
;databaseName= ';databaseName='
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^;]* any character except: ';' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
import java.util.regex.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = ";databaseName=([^;]*)";
final String string = "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false\n" + "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;integratedSecurity=false;databaseName=ab_cd_e";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Name: " + matcher.group(1));
}
}
}
Prints
Name: ab_cd_e
Name: ab_cd_e
答案3
得分: 0
从这里 https://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex
String mydata = "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false";
Pattern pattern = Pattern.compile("databaseName=(\\w+)(|;$)");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
输出:
> ab_cd_e
英文:
from here https://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex
String mydata = "jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false";
Pattern pattern = Pattern.compile("databaseName=(\w+)(|;$)");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
output:
> ab_cd_e
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论