简体   繁体   English

如何在 Python 中将这部分字符串与正则表达式匹配而无需后视需要固定宽度模式?

[英]How to match this part of the string with regex in Python without getting look-behind requires fixed-width pattern?

I want to extract the name from a create Table statement for example this:我想从 create Table 语句中提取名称,例如:

"CREATE OR REPLACE TEMPORARY TABLE IF NOT EXISTS author (" "创建或替换临时表如果不存在作者 ("

the name here is author.这里的名字是作者。 If you look into the official mariaDB documentation you will notice, that OR REPLACE, TEMPORARY and IF NOT EXISTS are optional parameters.如果您查看官方 mariaDB 文档,您会注意到 OR REPLACE、TEMPORARY 和 IF NOT EXISTS 是可选参数。

The regex I've come up with:我想出的正则表达式:

r"(?<=(create)\s*(or replace\s*)?(temporary\s*)?(table)\s*(if not exists\s*)?)(\w+)(?=(\s*)?(\())"

There also is no upper limit of how many spaces need to be between each word, but there is at least one required.每个单词之间需要多少个空格也没有上限,但至少需要一个。

When i try this regex on https://regexr.com/ it works with these examples(With flags case insensitive, multiline and global):当我在https://regexr.com/上尝试此正则表达式时,它适用于这些示例(带有标志不区分大小写、多行和全局):

CREATE TABLE book (
CREATE OR REPLACE TABLE IF NOT EXISTS author(
CREATE OR REPLACE TEMPORARY TABLE IF NOT EXISTS publisher ( 

在此处输入图像描述 在此处输入图像描述

However if i try to do this in python:但是,如果我尝试在 python 中执行此操作:

import re
firstRow = "CREATE OR REPLACE TABLE IF NOT EXISTS author ("
res = re.search(r"(?<=(create)\s(or replace\s)?(temporary\s)?(table)\s(if not exists\s)?)(\w+)(?=(\s)?(\())", firstRow, re.IGNORECASE)

it throws following error message:它抛出以下错误消息:

Traceback (most recent call last):
  File "test.py", line 116, in <module>
    res = re.sub(r"(?<=(create)\s(or replace\s)?(temporary\s)?(table)\s(if not exists\s)?)(\w+)(?=(\s)?(\())", firstRow, re.IGNORECASE)
  File "C:\Users\stefa\AppData\Local\Programs\Python\Python38-32\lib\re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "C:\Users\stefa\AppData\Local\Programs\Python\Python38-32\lib\re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\stefa\AppData\Local\Programs\Python\Python38-32\lib\sre_compile.py", line 768, in compile
    code = _code(p, flags)
  File "C:\Users\stefa\AppData\Local\Programs\Python\Python38-32\lib\sre_compile.py", line 607, in _code
    _compile(code, p.data, flags)
  File "C:\Users\stefa\AppData\Local\Programs\Python\Python38-32\lib\sre_compile.py", line 182, in _compile
    raise error("look-behind requires fixed-width pattern")
re.error: look-behind requires fixed-width pattern

It works as you have selected Javascript, which might support an infinite quantifier in a lookbehind assertion.它就像您选择了 Javascript 一样工作,它可能支持后向断言中的无限量词。

You don't need any lookarounds at all as you are already using a capture group, so you can match what is around the table name.您根本不需要任何环顾四周,因为您已经在使用捕获组,因此您可以匹配表名称周围的内容。

As all the \s* are optional, you might consider using word boundaries \b to prevent partial word matches.由于所有\s*都是可选的,您可以考虑使用单词边界\b来防止部分单词匹配。

\bcreate\b\s*(?:or replace\s*(?:\btemporary\s*)?)?\btable\s*(?:\bif not exists\s*)?\b(\w+)\s*\(

Regex demo正则表达式演示

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

相关问题 Python正则表达式后视需要固定宽度模式 - Python regex look-behind requires fixed-width pattern 正则表达式将唯一字符串提取到新列,出现错误“后视需要固定宽度模式” - Regex to extract unique string to new column, getting error "look-behind requires fixed-width pattern" Python - 错误:look-behind需要固定宽度模式 - Python - error: look-behind requires fixed-width pattern Python 正则表达式引擎 - “后视需要固定宽度的模式”错误 - Python Regex Engine - "look-behind requires fixed-width pattern" Error 寻找连续重复单词时,Python后视正则表达式“固定宽度模式”错误 - Python look-behind regex “fixed-width pattern” error while looking for consecutive repeated words .error: 后视需要固定宽度的模式(在加载 spacy 自定义模型时) - .error: look-behind requires fixed-width pattern (while loading spacy custom model) 正则表达式模式在不验证固定宽度模式的情况下无法使用后向查看 - Regex Pattern doesn't work using look behind without validating the fixed-width pattern 在 python 正则表达式中回顾 - look-behind in python regex python正则表达式具有不同的长度“或” - python regex with differing length “or” in look-behind 后视中的模式无效 - Invalid pattern in look-behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM