Post Reply 
 
Thread Rating:
  • 12 Votes - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
03-28-2016, 15:11
Post: #1
Paradox
0
Paradox you are a programmer right?

Can you check my code , it dosent work , it moves straight to else: part
put it should be in if part. Its an regualexpression what says that enterd number must be 5 digits and it must start with 1 or 2 or 3 or 4 or 5. When i type in 45892 what should say it is. But it dosent.

Code:
import re
post_number = input("Enter postnumber: ")
if re.match("^[1-5][0-4]{4}$",post_number):
    print("It is")
else:
    print("it isnt")

If youre still active here any help would be appreciated.

Find all posts by this user
Quote this message in a reply
03-29-2016, 21:21 (This post was last modified: 03-29-2016 21:33 by dage5.)
Post: #2
RE: Paradox
0
You had 0-4 in second part. This should work if you want it to be exactly 5 characters long and it will say true even if the number is surrounded with text like here: "fdf45892fddfd".
Code:
[1-5][0-9]{4}
If you add start and end of string it will match only if whole string is the number, so if you are searching in text it will never match.
Code:
^[1-5][0-9]{4}$
You can also do this. Tongue
Code:
(1|2|3|4|5)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2​|3|4|5|6|7|8|9)
Here is good thing for testing: http://regexr.com/
What is the langueage you are using btw?

Visit this user's website Find all posts by this user
Quote this message in a reply
03-30-2016, 08:35
Post: #3
RE: Paradox
0
Its Python programming language.
OMG DAGE Big Grin
Youre code work , man Big Grin Big Grin Big Grin

Find all posts by this user
Quote this message in a reply
03-30-2016, 19:53
Post: #4
RE: Paradox
0
Seems like dage was faster. Wink

Anyways like he already mentioned you got a mistake in your regex. As far as learning is concerned, I recommend the site https://regex101.com because it gives (or at least tries to give) an explanation of all parts of your regex. However, http://regexr.com (the one dage mentioned) is great too.

I'm mainly abroad at the moment and rather inactive. Currently in Russia (Moscow).

---------------------------------------------------------------------------------------------------------------
» I don't suffer from insanity. I enjoy every minute of it. «
---------------------------------------------------------------------------------------------------------------
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




» Powered by: betamaster.us «» Partner sites : vsee.us | knet.us |