如何用python解析oracle jdbc ezconnect

时间:2016-08-12 13:43:58

标签: python oracle parsing jdbc

我试图搜索如何使用python解析一个oracle jdbc字符串,该字符串可以采用不同的格式,但没有找到任何格式。

问题:

输入字符串可以基于不同的模式:

  • JDBC:预言:瘦:@ // hostname.example.ru:1521/database.example.ru
  • JDBC:预言:瘦:@hostname:1521:DATABASE

我无法预测下次我会得到什么样的模式。所以我需要使用一些总是能够解析这样的字符串的包来不再实现一个自行车。

Update_#0

仍未找到任何可以解析部件上的连接字符串而不实际连接到oracle的包。

目前,我编写了正则表达式来解析jdbc oracle ezconnect字符串,你可以用它来解析ezconnect:

^jdbc:oracle:thin:((?'username'[a-zA-Z0-9]{1,})([\/](?'password'[a-zA-Z0-9]{1,})){0,1}){0,1}@((\/\/){0,1}(?'hostname'[a-zA-Z0-9\.\-]*)(\:(?'port'\d+)){0,1})(\/(?'service_name'[a-zA-Z\.\-0-9]{1,}(\:(?'server_type'[a-zA-Z]{1,})){0,1}){0,1}(\/(?'instance_name'[a-zA-Z0-9]{1,})){0,1}){0,1}$

这是一个扩展查询:

^
    jdbc:oracle:thin:
    (
        (?'username'[a-zA-Z0-9]{1,})
        ([\/]
            (?'password'[a-zA-Z0-9]{1,})
        ){0,1}
    ){0,1}
    @
    (
        (\/\/){0,1}
        (?'hostname'[a-zA-Z0-9\.\-]{1,})
        (\:(?'port'\d+)){0,1}
    )
    (\/
        (?'service_name'[a-zA-Z\.\-0-9]{1,}
            (\:
                (?'server_type'[a-zA-Z]{1,})
            ){0,1}
        ){0,1}
        (\/
            (?'instance_name'[a-zA-Z0-9]{1,})
        ){0,1}
    ){0,1}
$

你可以在这一行测试它here

jdbc:oracle:thin:@//hostname.example.ru:1521/database.example.ru
jdbc:oracle:thin:@sales-server
jdbc:oracle:thin:@sales-server:3456
jdbc:oracle:thin:@sales-server/sales
jdbc:oracle:thin:@sales-server:80/sales
jdbc:oracle:thin:@sales-server/sales:dedicated/inst1
jdbc:oracle:thin:@sales-server//inst1
jdbc:oracle:thin:@sales-server:1521/sales.us.acme.com
jdbc:oracle:thin:@//sales-server/sales.us.acme.com
jdbc:oracle:thin:@//sales-server.us.acme.com/sales.us.oracle.com
jdbc:oracle:thin:wat@//sales-server.us.acme.com/sales.us.oracle.com
jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com/sales.us.oracle.com
jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com/sales.us.oracle.com:dedicated/instance
jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com//instance
jdbc:oracle:thin:@non-ezconnect-string-test:1521:DATABASE

Update_#1

此代码适用于python:

import re

jdbc_ezconnect = re.compile("^jdbc:oracle:thin:((?P<username>[a-zA-Z0-9]{1,})([\/](?P<password>[a-zA-Z0-9]{1,})){0,1}){0,1}@(?P<ezdb_name>((\/\/){0,1}(?P<hostname>[a-zA-Z0-9\.\-]{1,})(\:(?P<port>\d+)){0,1})(\/(?P<service_name>[a-zA-Z\.\-0-9]{1,}(\:(?P<server_type>[a-zA-Z]{1,})){0,1}){0,1}(\/(?P<instance_name>[a-zA-Z0-9]{1,})){0,1}){0,1})$", re.MULTILINE)

text = [
    "jdbc:oracle:thin:@//hostname.example.ru:1521/database.example.ru",
    "jdbc:oracle:thin:@sales-server",
    "jdbc:oracle:thin:@sales-server:3456",
    "jdbc:oracle:thin:@sales-server/sales",
    "jdbc:oracle:thin:@sales-server:80/sales",
    "jdbc:oracle:thin:@sales-server/sales:dedicated/inst1",
    "jdbc:oracle:thin:@sales-server//inst1",
    "jdbc:oracle:thin:@sales-server:1521/sales.us.acme.com",
    "jdbc:oracle:thin:@//sales-server/sales.us.acme.com",
    "jdbc:oracle:thin:@//sales-server.us.acme.com/sales.us.oracle.com",
    "jdbc:oracle:thin:wat@//sales-server.us.acme.com/sales.us.oracle.com",
    "jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com/sales.us.oracle.com",
    "jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com/sales.us.oracle.com:dedicated/instance",
    "jdbc:oracle:thin:wat/wat@//sales-server.us.acme.com//instance",
    "jdbc:oracle:thin:@hostname:1521:DATABASE"
]

matches = jdbc_ezconnect.search(text[0])

username = matches.group('username')
password = matches.group('password')
ezdb_name = matches.group('ezdb_name')
hostname = matches.group('hostname')
port = matches.group('port')
service_name = matches.group('service_name')
server_type = matches.group('server_type')
instance_name = matches.group('instance_name')


print username, password, ezdb_name, hostname, port, service_name, server_type, instance_name

输出:

None None //hostname.example.ru:1521/database.example.ru hostname.example.ru 1521 database.example.ru None None

0 个答案:

没有答案