python多行正则表达式以获取括号中的可选组 [英] python multiline regex to get optional group in parentheses

查看:19
本文介绍了python多行正则表达式以获取括号中的可选组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力编写一个正则表达式来基本上为两个表获得 4 个组

I am struggling to write a regex to basically get 4 groups for both tables

  1. 表名例如table_v1
  2. 表名后第一个 () 中的表列
  3. () 中的主键值
  4. 如果存在 CLUSTERING ORDER,则可以在 () 中选择值

我试过了,除了无法获取集群顺序值外,大部分都有效.

I tried this, mostly works except cannot get cluster order value .

这是一个失败的演示

re.compile("CREATE\s+TABLE\s+(?:[a-z][a-z0-9_]*).*?((?:[a-z][a-z0-9_"]*)).*?(\(.*?\)) WITH.*?(\(.*?\)).*?;").findall(string_below)

这是试图在正则表达式之上运行的字符串.

Here is the String trying to run above regex on.

CREATE TABLE abcdeg.table_v1 (
    "id" text,
    "obj" text,
    "version" bigint,
    output text,
    server text,
    PRIMARY KEY ("id", "obj", "version")
) WITH CLUSTERING ORDER BY ("id" ASC, "version" DESC)
    AND bloom_filter_fp_chance = 0.1
    AND comment = ''
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

CREATE TABLE abcdeg.result_v1 (
    "id" text,
    "obj" text,
    time int,
    PRIMARY KEY (("id", "obj"))
) WITH bloom_filter_fp_chance = 0.1
    AND comment = ''
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND speculative_retry = '99.0PERCENTILE';

CREATE TABLE abcdeg.result_v2 (
    "id" text PRIMARY KEY,
    "obj" text,
    time int
) WITH bloom_filter_fp_chance = 0.1
    AND comment = ''
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND speculative_retry = '99.0PERCENTILE';

推荐答案

我无法在 python 中测试它,但它在演示中有效.将表名捕获到组 1 中,将字段捕获到组 2 中,将主键(即使有双括号)捕获到组 3 中,如果存在聚类顺序,则捕获到 组 5

I can't test this in python, but it works in the demo. Captures table name into group 1, fields into group 2, primary keys (even with double parantheses) into group3, and if Clustering Order is present, into group 5

CREATE\sTABLE\s.+?\.(\w+)\s\(\n?(.+?)\n\s*PRIMARY\sKEY\s\(+([^)]*)\)+.*?(CLUSTERING\sORDER\sBY\s\(([^)]+)\)|;)

演示

这篇关于python多行正则表达式以获取括号中的可选组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆