从Jdbc URL中查找数据库名称的正则表达式

huangapple go评论73阅读模式
英文:

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 (?&lt;=databaseName=)

Hence, the regex (?&lt;=databaseName=)\\w+(?=;|$) gives you ab_cd_e

Try this:

Pattern pattern = Pattern.compile(&quot;(?&lt;=databaseName=)\\w+(?=;|$)&quot;, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jdbcUrl);
if (matcher.find()) 
	System.out.println(matcher.group());

答案2

得分: 1

使用

    ;databaseName=([^;]*)

见[证明][1]

**解释**

                             解释
    --------------------------------------------------------------------------------
      ;databaseName=           &#39;;databaseName=&#39;
    --------------------------------------------------------------------------------
      (                        分组并捕获到 :
    --------------------------------------------------------------------------------
        [^;]*                    除 &#39;;&#39; 之外的任何字符(0 次或多次
                                 (匹配最大数量)
    --------------------------------------------------------------------------------
      )                         的结束

[Java 代码][2]:

    import java.util.regex.*;
    
    class TestClass
    {
    	public static void main (String[] args) throws java.lang.Exception
    	{
    		final String regex = &quot;;databaseName=([^;]*)&quot;;
    	    final String string = &quot;jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false\n&quot; + &quot;jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;integratedSecurity=false;databaseName=ab_cd_e&quot;;
    	     final Pattern pattern = Pattern.compile(regex);
    	     final Matcher matcher = pattern.matcher(string);
    	     while (matcher.find()) {
    	       System.out.println(&quot;Name: &quot; + 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=           &#39;;databaseName=&#39;
--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    [^;]*                    any character except: &#39;;&#39; (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of 

Java code:

import java.util.regex.*;

class TestClass
{
	public static void main (String[] args) throws java.lang.Exception
	{
		final String regex = &quot;;databaseName=([^;]*)&quot;;
	    final String string = &quot;jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false\n&quot; + &quot;jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;integratedSecurity=false;databaseName=ab_cd_e&quot;;
	     final Pattern pattern = Pattern.compile(regex);
	     final Matcher matcher = pattern.matcher(string);
	     while (matcher.find()) {
	       System.out.println(&quot;Name: &quot; + 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 = &quot;jdbc:sqlserver://abc.com:1440;instanceName=MSSQLSERVER2019;databaseName=ab_cd_e;integratedSecurity=false&quot;;
Pattern pattern = Pattern.compile(&quot;databaseName=(\w+)(|;$)&quot;);
Matcher matcher = pattern.matcher(mydata);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

output:

> ab_cd_e

huangapple
  • 本文由 发表于 2020年9月2日 22:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定