Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 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').


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...