1

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);
Vladislav Varslavans
  • 2,775
  • 4
  • 18
  • 33
ashish gupta
  • 135
  • 5
  • 16
  • SO is not a "do it for me" service. Please show us what you have tried. – Vladislav Varslavans Sep 02 '20 at 14:29
  • @VladislavVarslavans yes, i have tried - Pattern pattern = Pattern.compile("^(;databaseName=)(.*?)([;])", Pattern.CASE_INSENSITIVE); – ashish gupta Sep 02 '20 at 14:32
  • You can try to get correct pattern with help of [this](https://regex101.com/) site. Please try it. – Vladislav Varslavans Sep 02 '20 at 14:34
  • 1
    Your first problem is the `^` means to start matching at the _beginning_ of your string. The other problem is your regex states that the databaseName _must_ be followed by a semicolon. Lastly, your full match has more information than you want, so you have to make sure you get the correct capture group; or better yet, use lookarounds like @Hülya did. – Gary Sep 02 '20 at 14:46
  • 1
    You could use a single capturing group with a negated character class instead of the non greedy dot to get the name. `;databaseName=([^;s]+)(?:;|$)` https://regex101.com/r/k8iZpD/1 or split the string on `;` and filter by databaseName – The fourth bird Sep 02 '20 at 14:54

3 Answers3

2

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());
Hülya
  • 3,353
  • 2
  • 12
  • 19
1

Use

;databaseName=([^;]*)

See proof

Explanation

                         EXPLANATION
--------------------------------------------------------------------------------
  ;databaseName=           ';databaseName='
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^;]*                    any character except: ';' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1

Java code:

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

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
0

from here 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

elbraulio
  • 994
  • 6
  • 15