0

I'm trying to match a URL using re but am having trouble in regards to making part of the match optional.

import re

x = raw_input('Link: ')
reg = '(http|https)://(iski|www\.iskis|iskis)\.(in|com)/[A-Za-z0-9?&=/?_]+'
if re.match(reg, x):
    print 'True'

Currently, the above code would match something like:

https://iskis.com/?loc=shop_view_item&item=220503032

I would like to alter the regular expression to make the following, [A-Za-z0-9?&=/?_]+ an option - As such, anything after the slash isn't required, so the following should match:

https://iskis.com

I'm sure there is a simple solution but I don't know how to go about solving this.

0

1 Answer 1

1
reg = '(http|https)://(iski|www\.iskis|iskis)\.(in|com)(/[A-Za-z0-9?&=/?_]+)?$'

Should do it. Surround the character class with () so it's a group, put a ? after it to make the text match 0-1 instances of that group, and put a $ at the end so that the regex will match to the end.

EDIT:

Come to think of it, you could use the optional match elsewhere in your regex.

reg = '(https?)://(www\.)?(iskis?)\.(in|com)(/[A-Za-z0-9?&=/?_]+)?$'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.