Skip to content
Advertisement

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 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.

The regex I’ve come up with:

JavaScript

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):

JavaScript

enter image description here enter image description here

However if i try to do this in python:

JavaScript

it throws following error message:

JavaScript

Advertisement

Answer

It works as you have selected Javascript, which might support an infinite quantifier in a lookbehind assertion.

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.

JavaScript

Regex demo

Advertisement