2

I have A string:

line_to_test = "http://website/[SequenceOfLetters&NumbersONLY].html"

I want a regex for matching the above pattern:

what i have tried currently is:

c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]?).html",line_to_test))

But c here comes to be null even when the line_to_test contains the pattern.

3
  • And what string did you tested with ? Commented Mar 24, 2016 at 10:27
  • ([a-zA-Z0-9]?) = 1 or 0 letter or number - if you want a sequence, you want to replace ? with +... Commented Mar 24, 2016 at 10:28
  • test_string = "sdfmknldksjfnkmsd f,nm http://abc.de/msndkjnaskl.html" Commented Mar 24, 2016 at 10:28

2 Answers 2

2

? means whatever preceded it was optional, in this case [a-zA-Z0-9]. That means you can have a letter or number either 0 or 1 times.

You should use the *, to select it 0 times or more, or use the +, to select it 1 times` or more.

Try this RegEx:

c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]+).html",line_to_test))

If you used a *, it would be the same as ([a-zA-Z0-9]+)?, meaning http://website/.html would work.

Live Demo on RegExr

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks For the precise explaination !!
0

? will only match 0 or 1 character. Try

c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]+).html",line_to_test))

You can use an online service like regexr to test your regexes: http://regexr.com/3d301

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.