Python: Checking if string follows wikipedia link format

2024/11/16 13:00:58

If I have a string named link, how would I go about checking to see if it follows the same format as a wikipedia URL? To clarify, wikipedia URLs (in this case) always begin with en.wikipedia.org/wiki/ They can have any character (including # signs and apostrophes after the /wiki/ and spaces are indicated with underscores. Also, they can have a word in parenthesis, for example: en.wikipedia.org/wiki/Sesame_Street(Elmo's_World). For example, if the string link looked like "en.wikipedia.org/wiki/Sesame_Street(Elmo's_World", that wouldn't match since there is no closing parenthesis. Thanks!

Answer

I think something like this can do what you want:

import re
link = 'en.wikipedia.org/wiki/Sesame_street(Elmo\'s_world)'
sub = re.sub(r'^.{2}\.wikipedia\.org/wiki/(.*)', r'\1', link)
if sub != link:if '(' in sub:if ')' in sub:print 'ok'else:print 'not ok'else:print 'ok'
else:print 'not ok'

But it just checks if there is ')' sign if paranthesis was opened so if it's opened twice and closed once it will match anyway, but maybe it will help you work something out. (Btw. it will also match other languages, if only 'en' should be matched change {2} to 'en').

https://en.xdnf.cn/q/119190.html

Related Q&A

Create dictionary comprehension from list with condition syntax

Id like to create a dictionary using dictionary comprehension syntax.Note that list l contains tuples of strings and tuples with 1st element always a time stamp.This works:d = {} for entry in l:if entr…

How to find next empty cell on a specific column and write to it?

I want to find the next empty cell in a specific column and write to values that cell. Ive tried it using following method: for row in sheet[A{}:A{}.format(sheet.min_row,sheet.max_row)]:if row is None:…

How can i pass data from template in Django to Javascript in this specific case

I am using Google map api and i am trying to pass the data (longitude and latitude) to the template then use the data in the javascript to show a specific location.location.html{% for venue in propert…

Python Highscores w/ text file

i am currently working on a text based game, and have run into a problem trying to make my last function, highscore. My problem is this, i would like to make the function save my top five scores and sa…

Do any one know about this Error in python? how can I resolve this?

I am plotting the data with MapBoxGl Python Library on maps, here is my code which is taking the latitude, longitude and points from the Pandas DataFrame and trying to make the geojson, here is the cod…

Pandas: Count Higher Ranks For Current Experiment Participants In Later Experiments (Part 1)

Learning Experiments In a series of learning experiments, I would like to count the number of participants in each experiment that improved their performance in subsequent experiments (Rank 1 is highes…

Pass variables into and out of exec [closed for not being clear. Modified and reuploaded] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

List Object Not Callable, Syntax Error for Text-Based RPG

Im having issues (still) with generating a random object, in this case a random herb found by foraging. Heres the code for the function:def collectPlants(self):if self.state == normal:print"%s spe…

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…