Home > Back-end >  How to create a regex that begins with specific characters and ends at a space?
How to create a regex that begins with specific characters and ends at a space?

Time:03-18

I have been trying this for several days with limited success. I need to go through a pandas dataframe and retrieve the names of SQL global temp tables from query text.

So far, I have this, which almost works:

##.*\s

As is, if given a string (yes, it's a dummy) such as SELECT * FROM ##GlobalTempTable1 more stuff

will return

##GlobalTempTable1 more

I do not understand why it extends beyond the desired text.

CodePudding user response:

I would use str.extract() here with the pattern ##\S :

df["table_name"] = df["query"].str.extract(r'(##\S )')

If you really want to stick with your current pattern, then make the dot lazy and use ##.*?\s. But keep in mind this will only work if the table name is followed by something else, e.g. a WHERE clause.

CodePudding user response:

I don't know about panda and dataframe, but if you're solving via regex, then you can write as ##.*?\s. The one you've written is greedy search, it will match till last \s, but one i have given will stop match when it encounters first \s

  • Related