Home > Net >  How to extract the substring from the beginning to a character?
How to extract the substring from the beginning to a character?

Time:02-16

I want to use Python 3 regex to extract a substring of URL from the beginning to the character ?.

input: url = 'https://www.reuters.com/article/us-health-coronavirus-mexico-city/mexico-city-to-begin-gradual-exit-from-lockdown-on-monday-idUSKBN23K00R?feedType=RSS&feedName=worldNews'

expected output: 'https://www.reuters.com/article/us-health-coronavirus-mexico-city/mexico-city-to-begin-gradual-exit-from-lockdown-on-monday-idUSKBN23K00R'

I tried the following:

import re
end = article_urls[0].find('?')
article_urls[0][:end]

CodePudding user response:

You could use a simple split.

beginning, end = url.split('?')
  • Related