I want to remove all words before a specific word. But in my sentence there are some specific word. the following example:
dvdrentalLOG: statement: SELECT email, actor.last_name, count(actor.last_name) FROM (SELECT email, actor_id FROM (SELECT email, film_id FROM (SELECT email, inventory_id FROM customer as cu JOIN rental ON cu.customer_id = rental.customer_id ORDER BY email) as sq JOIN inventory ON sq.inventory_id = inventory.inventory_id) as sq2 JOIN film_actor ON sq2.film_id = film_actor.film_id) as sq3 JOIN actor ON sq3.actor_id = actor.actor_id GROUP BY email, actor.last_name ORDER BY COUNT(actor.last_name) DESC
In the example above, I want to remove all the words before the first SELECT. I've already tried this How to remove all characters before a specific character in Python?
Any idea what I need to do?
You can use this regex and replace with empty string:
^.+?(?=SELECT)
like this:
result = re.sub(r"^.+?(?=SELECT)", "", your_string)
Explanation:
Because you want to remove everything that's before the first SELECT
, the match is going to start at the start of the string ^
. And then you lazily match any character .+?
, until you see SELECT
.
Alternatively, remove the lookahead and replace with SELECT
:
result = re.sub(r"^.+?SELECT", "SELECT", your_string)
EDIT:
I found yet another way to do this, with partition
:
partitions = your_string.partition("SELECT")
result = partitions[1] + partitions[2]
If you are concerned only with 1st occurence of word it is easy to do. Consider following example
import re
txt = 'blah blah blah SELECT something SELECT something another SELECT'
output = re.sub(r'.*?(?=SELECT)','',txt,1)
print(output) #SELECT something SELECT something another SELECT
I used so called zero-length assertion inside pattern, so it is match only if SELECT
follow and I give 1
as 4th re.sub
argument meaning that there will be only 1 substitution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.