Email Verification using re.search Python - python

Need help making email verifications with the variable 'pattern' and making it so that it loops if it doesn't contain whatever is within the pattern. Required to use re.search. I tried a couple of things for the last hour and this is where I'm kind of lost.
import re
pattern = '[a-zA-Z0-9]' + '[a-zA-Z0-9]'+'#[a-zA-Z]'+'(.com/.edu/.net)'
user_input = input('Enter Your Email:')
while user_input is not pattern:
if (re.search(pattern,user_input)):
print(re.seach(pattern,user_input))
print('Valid Email:'+ user_input)
else:
print(re.search(pattern,user_input))
print('Invalid Email:'+ user_input)
user_input = input('Enter Your Email:')```

The code is great, but the pattern lacks a bit of functionality. In fact for e-mail addresses, it misses the dash - and the underscore _. Luckily, you can just say to match \w. It is the same as if you would have specified [a-zA-Z0-9_]. (it still misses the dash though, so your approach is good but too short.) Anyway, there are a few further things that an address should meet.
it must start with a alphabetic character
While theoretically, the address could be composed of a single character at the start and only to after the # sign, and be almost infinitely long, it is highly unlikely
I suggest the pattern
'[a-zA-Z]+[a-zA-Z0-9_\-]{0,42}#[a-zA-Z]{2,42}\.((com)|(edu)|(net))\b?'
Limiting the number of characters with '{m,n}' lets you ensure that you won't have an overflow error when storing the address. Well and addresses shorter than 'a#bc.st' simply don't exist as at least two characters are required.
Lastly, the or-operator applies only to the immediate adjoin characters, so you need to group the mail extensions:
((com)|(edu)|(net))
import re
pattern = '[a-zA-Z]+[a-zA-Z0-9_\-]{0,42}#[a-zA-Z]{2,42}\.((com)|(edu)|(net))\b?'
while True:
user_input = input('Enter Your Email:')
if re.match(pattern, user_input):
print(re.search(pattern,user_input))
print('Valid Email:'+ user_input)
break
else:
print(re.match(pattern,user_input))
print('Invalid Email:'+ user_input)
I think, it is better if you use re.match() as it matches the string right from the start. Usually one doesn't like if you end up with 1abc#def.comm to be a valid address (because re.search() would find the valid string abc#def.com. With the same argumentation, you should add a \b to the end of the pattern

I made a slight modification to your pattern and to your code.
import re
pattern = '[a-zA-Z0-9]+#[a-zA-Z]+(\.com|\.edu|\.net)'
while True:
user_input = input('Enter Your Email:')
if (re.search(pattern,user_input)):
print(re.search(pattern,user_input))
print('Valid Email:'+ user_input)
break
else:
print(re.search(pattern,user_input))
print('Invalid Email:'+ user_input)
Here's an example run:
Enter Your Email:fred
None
Invalid Email:fred
Enter Your Email:mark#so.com
<re.Match object; span=(0, 11), match='mark#so.com'>
Valid Email:mark#so.com

Related

How to check if string contains only valid amount of spaces

I need to write code that checks if a certain input string is valid. It must:
Contain a "space" in the input string which separates words
Not contain multiple consecutive spaces in a row
Not contain just a "space" (just
single space as an input).
Here is what I mean:
username = str(input())
print(username)
username = "two apples" # acceptable
username = "two apples and pears" # acceptable
username = "two' '' 'apples" # not acceptable (because of 2 spaces in a row or more)
username = " " # not acceptable (because of single space with no other words.)
username = "' '' '' '' '' '' '" #not acceptable because of multiple spaces (didn't know how to type it in here better to clarify.
I recognize that this might be slightly different than what you are directly asking for, but why not strip the extra spaces from the username input? You could use regex to quickly strip them out per this answer.
If you simply want to return an invalid input, I would again use regex.
import re
username = str(input("Please enter your username: "))
if re.search(" {2,}", username):
print("Please do not include multiple spaces in a row in your username!")
else:
# Do the rest of your program.
I was having my online class, so I couldn't do on time. Now I can give the the answer.
Here's the code. It's pretty simple to understand too.
username = input()
if (" ") in username:
print("Eh!")
elif username.isspace():
print("Ew!")
else:
print(username)
And BTW you don't need to use str() in the input as it takes a string input by default.
Here is the code you are looking for I believe. Please test with different test cases and comment if something is wrong. I tried with all your test cases.
def checkspace(string):
if string:
for idx,i in enumerate(string):
try:
if (string[idx] == ' ' and string[idx+1] ==' '):
return 'String is Not Perfect'
except:
return 'String is Not Perfect'
print('String is Perfect')
else:
return 'No String At AlL'
Eg:
string="two apples"
checkspace(string)
output:
"String is Perfect"
Eg:
string="two apples"
checkspace(string)
output:
"String is Not Perfect"

how to use variables in Regex

I am trying to write a function that checks for a strong password. The password must contain one upper case, one lower case,a number and must be 8 characters long.
import re
def checker():
while True:
userInput = input(' Please input a password ')
passwordRegex = re.compile(r'[a-zA-Z0-9]+ {,8}')
match = passwordRegex.search(userInput)
if match:
print('Good!')
else:
print('Bad!')
checker()
This function always outputs Bad even when the password meets all the requirements. I have a feeling the error has to do with how I am using my Regex and Variables. I am using python 3.6.
Expanding on the answer from here:
passwordRegex = re.compile("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\S{8,}")
check out this demo
Using lookaheads we make sure there is at least one character from each group, then require at least 8 characters total. Note that you can customize the allowed characters (if you want to allow symbols) by changing the last group, the one before {8,}
Based on the feedback from #Aran-Fey and #Tomerikoo, i have updated my code and it works now.
import re
def checker():
while True:
userInput = input(' Please input a password ')
passwordRegex = re.search(r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$',userInput)
if passwordRegex:
break
print('Good!')
checker()

Are all these IF's and None's the best way to handle RE?

I have been trying to make my own, very simple program to check if the text string the user has copied can be considered a strong password, based on the regular exressions. In the program, I wanted a strong password to be considered to have at least 8 characters, at least one lower and one upper case character, and also at least one digit.
The code I wrote looks like this:
import re, pyperclip
# regex for password
regexEight = re.compile(r"\w{8,100}") # regex for 8 char.
regexLower = re.compile(r'[a-z]') # regex for lower.
regexUpper = re.compile(r"[A-Z]") # regex for upper.
regexNum = re.compile(r"\d") # regex for number.
# get text from paste
text = str(pyperclip.paste())
# see if text matches every regex.
mo = regexEight.search(text)
if mo != None:
exit
else:
print("Password to short.")
mo2 = regexLower.search(text)
if mo2 != None:
exit
else:
print("Password need to contain at least one lower case character.")
mo3 = regexUpper.search(text)
if mo3 != None:
exit
else:
print("Password need to contain at least on upper case character.")
mo4 = regexNum.search(text)
if mo4 != None:
exit
else:
print("Password need to contain at least one digit.")
# return this if every regex matches.
if mo or mo2 or mo3 or mo4 != None:
print("You have a strong password.")
I'm a complete beginner at RE so I used None to see if the object was matching or not (if it matched it returned the particular password, if it didn't mo(1,2,3) = None). However, I kind of feel that this way is quite unusual, or at least I don't think that is how RE should be handled, so that is why I asked here.
Is there any way to make this code simpler? Or is this way quite OK for a program? In my opinion it feels like the code would be better without all the if's and the None's. Is there a way to get rid of them?
I think a decent general approach would be to create some structure which holds the regexes that you want to check and the corresponding error messages.
import re
tests = [
(re.compile(r"\w{8,100}"), "Too short."),
(re.compile(r"[a-z]"), "Add lowercase letter."),
(re.compile(r"[A-Z]"), "Add uppercase letter."),
(re.compile(r"\d"), "Add number.")
]
check = True
for regex, message in tests:
if regex.search("example_password") is None:
print(message)
check = False
if check:
print("Strong password.")
You're making this way more complex than it needs to be:
def validate_password(password):
if not password or len(password) < 8:
print("Password too short!")
elif password == password.lower() or password == password.upper():
print("Password must contain at least one lowercase and one uppercase character!")
elif not any(c.isdigit() for c in password):
print("Password must contain at least one digit!")
else:
return True # all is well
return False # didn't validate

re.rompile returns true with false, not allow symbols

Im trying to use regex do check a variable for accepted letters and numbers. This is my def:
def special_match(strg, search=re.compile(r'[a-z0-9]').search):
if bool(search(strg)) is True:
print ('Yes: ' + strg)
elif:
print ('nej: ')
while 1:
variabel = raw_input('Enter something: ')
special_match(variabel)
sys.exit()
And it seems that is accepts not allow symbols in combination with allow symbols:
Enter something: qwerty
Yes: qwerty
Enter something: 1234
Yes: 1234
Enter something: !!!!
nej!
Enter something: 1234qwer!!!!
Yes: 1234qwer!!!!
Enter something:
The last one should not be accepted.. What I'm doing wrong??
All your regular expression search is doing is checking to see if at least one of the characters is present.
If you want to require that the entire string contains nothing but those characters, then you can use:
r'^[a-z0-9]*$'
That anchors the pattern at both the start and end of the string, and only matches if all of the characters in between are in the specified set of characters.
Note that this will also match the empty string. If you wish to require at least one character, then you can change the * to +.
the search method finds for regex you gave and if it finds then returns a Match object here 1234qwer!!! has [a-z0-9] but !!!! doesnt.
Try a!!!. that will also return True.
You could try doing
re.search(r"[^a-z0-9]",word)
and if this returns True that means your word has something other than digits and alphabets and that should be rejected.
NOTE: ^ means not.
The only thing that regex does is check that there is a number or a letter in your string. If you want to check that it only has numbers and letters, you need to anchor your pattern at the start and end, and add a repeat: r'^[a-z0-9]+$'
Note though that there is no need to use regex for this: the string isalnum() method will do the same thing.
There are a couple of other odd things in your code; you should definitely not be compiling a regex in the function signature and passing on the resulting search method; also you should not be converting the result to bool explicitly, and you should not compare bools with is True. A more Pythonic version, assuming you wanted to stick to the regex, would be:
def special_match(strg, search=None):
if not search:
search = re.compile(r'[a-z0-9]').search
if search(strg):
print ('Yes: ' + strg)
else:
print ('nej: ')
Also note elif is a syntax error on its own.

check user_input with if token in a loop

I am trying to write a function that checks my input to see whether I have entered the character '?'.
This is what I got so far:
def check_word():
word = []
check = 0
user_input = input('Please enter a word that does not contain ?: ')
for token in user_input.split():
if token == '?':
print('Error')
check_word()
My input: hello?
It is supposed to show 'Error'. But it doesn't show anything. Could you please tell me what wrong it is in my code.
I would use the in operator to do this
def check_word(s):
if '?' in s:
print('Error')
For example
>>> check_word('foobar')
>>> check_word('foo?')
Error
The problem is how you split the string of the user_input.
user_input.split():
The example doesn't contain whitespaces so the condition isn't met. If you want for example to check a sentence with spaces, you should split it like this: user_input.split(' ') to split it on the spaces.
But for this example you have two choices:
1) You can just iterate over the input itself because you want to check every char in the string for whether it's a ?.
That is, change user_input.split(): into simply user_input without splitting. This option is good if you might ever want to add some sort of action for each char.
2) It's very easy just to use in, like this:
if '?' in s:
print('There is a question mark in the string')
This is a very simple solution that you can expand and check for other chars in the string as well.
It's because user_input.split() splits the user_input by whitespace. Since hello? does not contain any whitespaces, token is equal to your input and the loop is executed once.
You should iterate over user_input instead, or simply check if '?' in user_input.

Categories