简体   繁体   English

在Regex中查找组 - Python

[英]Finding groups in Regex - Python

I am not able to understand the output of following code I just wrote. 我无法理解我刚刚编写的以下代码的输出。 I am trying to extract the complete value of the group in a string value. 我试图在字符串值中提取组的完整值。

>> str1 = "CREATE TABLE TABLE1 (NAME VARCHAR, PHONE VARCHAR) as SELECT NAME, PHONE FROM SCHEMA1.ADDRESS"
>> pat1 = re.compile("CREATE TABLE .+ FROM (\w).+")
>> ret= pat1.search(str1)
>> ret.groups()
('S',)

I was expecting the value to be SCHEMA1 . 我期待价值是SCHEMA1 How can I retrieve the entire value ? 如何检索整个值?

Why I am doing this? 为什么我这样做?

I have a file filled with DML statements intended for different schemas. 我有一个文件,里面装满了用于不同模式的DML语句。 So, I need to create separate files for each schema with DML statement from the file mentioned before. 因此,我需要使用前面提到的文件中的DML语句为每个模式创建单独的文件。

You meant to write: 你打算写:

pat1 = re.compile("CREATE TABLE .+ FROM (\w+)\..+")

\\w captures only a single character; \\w只捕获一个字符; you want to capture all the characters until the dot. 你想捕获所有的字符,直到点。 \\. represents the dot, and .+ represents the rest of the line ( address ). 表示点,而.+表示行的其余部分( address )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM