Which method to check if string index is queal to character.
I'd like to check if there's "@" in a string and wondering if any method is better/safer than others. I was told on one occasion that I should use is than ==, so how would be on this example. s = 't...@mail.is' I want check if string is a valid email address. code ''' import time email = "t...@mail.tu" start_time = time.time() for i in range(len(email)): print('@' in email[i]) print ("My program took", time.time() - start_time, "to run") print('--') start_time = time.time() for i in range(len(email)): print(email[i] == '@') print ("My program took", time.time() - start_time, "to run") print('--') start_time = time.time() if '@' in email: print('True') print ("My program took", time.time() - start_time, "to run") ''' -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: > > I'd like to check if there's "@" in a string and wondering if any method > is better/safer than others. I was told on one occasion that I should > use is than ==, so how would be on this example. > > s = 't...@mail.is' You could do simply if "@" in s: but probably what you really want is a regular expression. -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-28 16:31, Bischoop wrote: I'd like to check if there's "@" in a string and wondering if any method is better/safer than others. I was told on one occasion that I should use is than ==, so how would be on this example. [snip] The shortest and quickest way to check whether "@" is in my_string is: "@" in my_string -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-28, Stefan Ram wrote: > > "@" in s > That's what I thought. >>I want check if string is a valid email address. > > I suggest to first try and define "valid email address" in English. > > A valid email address consists of an email prefix and an email domain, both in acceptable formats. The prefix appears to the left of the @ symbol. The domain appears to the right of the @ symbol. For example, in the address exam...@mail.com, "example" is the email prefix, and "mail.com" is the email domain. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/2020 11:31 AM, Bischoop wrote: I'd like to check if there's "@" in a string Use the obvious "'@' in string". > and wondering if any method is better/safer than others. Any special purpose method built into the language is likely to be fastest. Safest? What danger are you worried about? I was told on one occasion that I should use is than ==, so how would be on this example. 'is' is for detecting an exact match with a particular, singular object. In particular, None, False, or True or user created objects meant for such use. s = 't...@mail.is' I want check if string is a valid email address. There are two levels of validity: has the form of an address, which is much more complicated than the presence of an '@', and corresponds to a real email account. code ''' import time email = "t...@mail.tu" start_time = time.time() for i in range(len(email)): print('@' in email[i]) This scans the entire string in a slow way, then indirectly performs '@' == char in a slow way. print ("My program took", time.time() - start_time, "to run") print('--') start_time = time.time() for i in range(len(email)): print(email[i] == '@') Slightly better, does comparison directly. for c in email: print(c == '@') Faster and better way to scan. for c in email: print(c == '@') break Stops at first '@'. '@' in email does the same, but should be slightly faster as it implements loop and break in the interpreter's implementation language. print ("My program took", time.time() - start_time, "to run") print('--') start_time = time.time() if '@' in email: print('True') print ("My program took", time.time() - start_time, "to run") ''' -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On Tue, Dec 29, 2020 at 6:18 AM Bischoop wrote: > > On 2020-12-28, Stefan Ram wrote: > > > > "@" in s > > > > That's what I thought. > > >>I want check if string is a valid email address. > > > > I suggest to first try and define "valid email address" in English. > > > > > > A valid email address consists of an email prefix and an email domain, > both in acceptable formats. The prefix appears to the left of the @ symbol. > The domain appears to the right of the @ symbol. > For example, in the address exam...@mail.com, "example" is the email prefix, > and "mail.com" is the email domain. > To see if it's a valid email address, send email to it and get the person to verify receipt. Beyond that, all you can really check is that it has an at sign in it (since a local address isn't usually useful in contexts where you'd want to check). So the check you are already looking at is sufficient. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 10:46 AM, Marco Sulla wrote: On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: I'd like to check if there's "@" in a string and wondering if any method is better/safer than others. I was told on one occasion that I should use is than ==, so how would be on this example. s = 't...@mail.is' You could do simply if "@" in s: but probably what you really want is a regular expression. Will add that Yes, you should always validate your inputs, but No, the presence of an @ sign in a text string is not sufficient to know it's a valid email address. Unfortunately validating that is hard. -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 3:08 PM, Mats Wichmann wrote: > On 12/28/20 10:46 AM, Marco Sulla wrote: >> On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: >>> >>> I'd like to check if there's "@" in a string and wondering if any >>> method >>> is better/safer than others. I was told on one occasion that I should >>> use is than ==, so how would be on this example. >>> >>> s = 't...@mail.is' >> >> You could do simply >> >> if "@" in s: >> >> but probably what you really want is a regular expression. >> > > Will add that Yes, you should always validate your inputs, but No, the > presence of an @ sign in a text string is not sufficient to know it's > a valid email address. Unfortunately validating that is hard. > Validating that it meets the SYNTAX of an email address isn't THAT hard, but there are a number of edge cases to worry about. Validating that it is a working email address (presumably after verifying that it has a proper syntax) is much harder, and basically requires trying to send to the address, and to really confirm that it is good requires them to do something actively with a message you send them. And then nothing says the address will continue to be valid. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 10:37 AM, Bischoop wrote: > A valid email address consists of an email prefix and an email domain, > both in acceptable formats. The prefix appears to the left of the @ symbol. > The domain appears to the right of the @ symbol. > For example, in the address exam...@mail.com, "example" is the email prefix, > and "mail.com" is the email domain. Seems so simple, yet at least half the web sites I try to use get it wrong. There's an entire RFC on this topic. Drives me crazy when a web site insists that myaddress+suf...@domain.com is not a valid address. It certainly is! -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 10:46 AM, Marco Sulla wrote: > On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: >> >> I'd like to check if there's "@" in a string and wondering if any method >> is better/safer than others. I was told on one occasion that I should >> use is than ==, so how would be on this example. >> >> s = 't...@mail.is' > > You could do simply > > if "@" in s: > > but probably what you really want is a regular expression. https://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 29/12/2020 09:27, Richard Damon wrote: On 12/28/20 3:08 PM, Mats Wichmann wrote: On 12/28/20 10:46 AM, Marco Sulla wrote: On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: ... but probably what you really want is a regular expression. because... Will add that Yes, you should always validate your inputs, but No, the presence of an @ sign in a text string is not sufficient to know it's a valid email address. Unfortunately validating that is hard. Validating that it meets the SYNTAX of an email address isn't THAT hard, but there are a number of edge cases to worry about. Validating that it is a working email address (presumably after verifying that it has a proper syntax) is much harder, and basically requires trying to send to the address, and to really confirm that it is good requires them to do something actively with a message you send them. And then nothing says the address will continue to be valid. (am assuming the in/is question has been answered) Looking at my email server log, I see messages addressed to "fd0d8f761...@danceswithmice.info" - which have been rejected. That spurious-address features an "@". It is a perfectly-formed email address. The domain-name is correct - and can be quickly verified as in-existence. However the account-name is a total fabrication. (is someone trying to put a 'hex' on me?) Accordingly, the advice that the only way to check if an email address is 'correct', is to see if it is accepted by the receiving-server. However, you can't (shouldn't be able to!) pierce that veil, to be able to prove/see for yourself! That is why many subscription-systems send an initial email message and ask the receiver to confirm receipt by 'clicking on the link provided'! Per previous RegEx discussions 'here': there are plenty of examples/attempts floating around the Internet. Absolutely none of which comes with a guarantee! To quote the famous con-man/men/women: if you believe any such stories, come and see me about buying into the Brooklyn Bridge/some agricultural (marsh) land/the next best thing... For many years, many of the RegEx-s circulating were so precise that they only recognised the original TLDs such as .com and .org. For years after .info was introduced, sites would tell me that my email address was 'illegal'. Leaving me to ask: do I really exist?, am I but part of some imaginary universe? (no comments about my Hobbit-feet, please!) After such disparagement it is worth remembering that there are checks and there are checks! It depends upon the purpose of the check, or the level-of-detail/accuracy desired! When accepting user-data it *is* worth (even "necessary") performing a 'sanity-check'. Per @Chris, if the user doesn't enter two 'words' separated by an @-sign, and the second 'word' doesn't include at least one dot/period/stop, then it seems quite probable that our user has made a typo! This is why some sites require an email address to be entered twice. (but copy-paste anyone?) Going much further than a typo-reducing/sanity-check is, per @Richard, considerably harder - and ultimately cannot guarantee an address. Thus, indulges in a sub-field of cyber-alchemy! -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 28Dec2020 13:08, Mats Wichmann wrote: >On 12/28/20 10:46 AM, Marco Sulla wrote: >>On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: >>>I'd like to check if there's "@" in a string and wondering if any >>>method is better/safer than others. Others have pointed out: '@' in s >Will add that Yes, you should always validate your inputs, but No, the >presence of an @ sign in a text string is not sufficient to know it's a >valid email address. Unfortunately validating that is hard. Validating that it is a functional email address is hard, involves delivering email and then finding out if it was delivered. A proper syntax check requires parsing an RFC5322 address grammar: https://tools.ietf.org/rfcmarkup/5322#section-3.4 Fortunately, Python ships with one of those in the email.utils module. The parseaddr() function will parse a single address, and getaddresses() will parse a list of addresses such as might be in a To: header line. They work well - I've been filing my email based on these for years - MANY thousands of messages. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On Tue, Dec 29, 2020 at 8:57 AM dn via Python-list wrote: > After such disparagement it is worth remembering that there are checks > and there are checks! It depends upon the purpose of the check, or the > level-of-detail/accuracy desired! > > When accepting user-data it *is* worth (even "necessary") performing a > 'sanity-check'. Per @Chris, if the user doesn't enter two 'words' > separated by an @-sign, and the second 'word' doesn't include at least > one dot/period/stop, then it seems quite probable that our user has made > a typo! This is why some sites require an email address to be entered > twice. (but copy-paste anyone?) I wouldn't even ask for a dot in the second half, actually. Yes, it's uncommon to have no dot, but it's fully legal, and not worth wrestling with. Considering the vast number of typos that *wouldn't* trip a filter, and the relatively small number that would, it's generally not worth putting too much validation in. Checking for the presence of "@" is a good way to check that it's an email address and not, say, the URL of someone's webmail service, but other than that, there's really not a lot that's worth checking. OTOH, if you're trying to recognize email addresses in plain text (say, in chat messages) so that you can autolink them, that's completely different. Same with URLs (eg https://example.com/foo) - you, as a human, can see that the URL should have ended at the "/foo", but technically "/foo)" is perfectly legal. So if you're validating, "/foo)" should be permitted, but if you're detecting, it should stop at "/foo". ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 4:52 PM, Michael Torrie wrote: > On 12/28/20 10:37 AM, Bischoop wrote: >> A valid email address consists of an email prefix and an email domain, >> both in acceptable formats. The prefix appears to the left of the @ symbol. >> The domain appears to the right of the @ symbol. >> For example, in the address exam...@mail.com, "example" is the email prefix, >> and "mail.com" is the email domain. > Seems so simple, yet at least half the web sites I try to use get it > wrong. There's an entire RFC on this topic. Drives me crazy when a web > site insists that myaddress+suf...@domain.com is not a valid address. > It certainly is! Yes, it really is fairly straight forward to do it right, but it does have details that need to be checked carefully (It is significantly harder if the email address can also contain comments or display names, but just a base email address isn't that hard). Many people do still get it wrong. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-28, Bischoop wrote: > On 2020-12-28, Stefan Ram wrote: >> >> "@" in s >> > > That's what I thought. > >>>I want check if string is a valid email address. >> >> I suggest to first try and define "valid email address" in English. > > A valid email address consists of an email prefix and an email domain, > both in acceptable formats. Well, that's assuming that bang routing/addresses aren't allowed. :) -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 1:27 PM, Richard Damon wrote: > Validating that it meets the SYNTAX of an email address isn't THAT hard, > but there are a number of edge cases to worry about. Yes one would think that, but in my experience half of all web sites get it wrong, insisting that my perfectly valid and RFC-compliant email address is not a proper email address. -- https://mail.python.org/mailman/listinfo/python-list
RE: Which method to check if string index is queal to character.
This may be a nit, but can we agree all valid email addresses as used today have more than an @ symbol? I see it as requiring at least one character before the @ that come from a list of allowed characters (perhaps not ASCII) but does not include the symbol @ again. It is normally followed by some minimal number of characters and maybe a period and one of the currently valid domains like .com or .it but the latter gets tricky as it can look like u...@abd.def.att.com or other long variations where only the final component must be testable in the program. The lack of an at-sign suggests it is not an email address. The lack of anything before or after also seems to disqualify it. You may be able to add more conditions but as noted, having more than one at-sign may also disqualify it. I am sure someone has some complex regular expressions that they think matches only potentially valid strings but, of course, as noted by Chris, to really validate that an address works might require sending something and validating a human replied and that can be quite task. -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Monday, December 28, 2020 2:24 PM To: Python Subject: Re: Which method to check if string index is queal to character. On Tue, Dec 29, 2020 at 6:18 AM Bischoop wrote: > > On 2020-12-28, Stefan Ram wrote: > > > > "@" in s > > > > That's what I thought. > > >>I want check if string is a valid email address. > > > > I suggest to first try and define "valid email address" in English. > > > > > > A valid email address consists of an email prefix and an email domain, > both in acceptable formats. The prefix appears to the left of the @ symbol. > The domain appears to the right of the @ symbol. > For example, in the address exam...@mail.com, "example" is the email > prefix, and "mail.com" is the email domain. > To see if it's a valid email address, send email to it and get the person to verify receipt. Beyond that, all you can really check is that it has an at sign in it (since a local address isn't usually useful in contexts where you'd want to check). So the check you are already looking at is sufficient. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On Tue, Dec 29, 2020 at 10:08 AM Avi Gross via Python-list wrote: > > This may be a nit, but can we agree all valid email addresses as used today > have more than an @ symbol? > > I see it as requiring at least one character before the @ that come from a > list of allowed characters (perhaps not ASCII) but does not include the > symbol @ again. It is normally followed by some minimal number of characters > and maybe a period and one of the currently valid domains like .com or .it > but the latter gets tricky as it can look like u...@abd.def.att.com or other > long variations where only the final component must be testable in the > program. There can be an @ in the first part of the address, and the domain may well not have a dot. > The lack of an at-sign suggests it is not an email address. The lack of > anything before or after also seems to disqualify it. You may be able to add > more conditions but as noted, having more than one at-sign may also > disqualify it. Lack of an at sign means it's a local address that can't be routed over the internet, and in many contexts, it's reasonable to exclude those. But two isn't illegal. > I am sure someone has some complex regular expressions that they think > matches only potentially valid strings but, of course, as noted by Chris, to > really validate that an address works might require sending something and > validating a human replied and that can be quite task. > Yes, many such regexes exist, and they are *all wrong*. Without exception. I don't think it's actually possible for a regex to perfectly match all (syntactically) valid email addresses and nothing else. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: Which method to check if string index is queal to character.
Thanks, Chris, I am not actually up-to-date on such messaging issues but not shocked at what you wrote. Years ago I recall most messages going out of my workplace looked like machine!machine2!ihnp4!more!evenmore!user with no @ in sight and as you mention, you may want to send to a domain and have it send to a subdomain so a multiple @ may make sense and so on. I note we have some places like groups.io that disguise the @ in your original email address so you can still see who it is from, even though it is in some sense from them but to actually use the email address in your own mailer, you need to substitute it back in. I think we all agree that unless there is further standardization, an email address can easily be rejected that is otherwise usable in some context and that one in proper format (by some definition) will fail in that context. The original question actually focused more narrowly on a good way to find if a character existed in a string for which regular expressions need not apply and most email addresses re short enough that techniques to speed up the search may not be useful unless all the program does is search millions of email addresses for the presence. Dropping out, ... -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Monday, December 28, 2020 8:02 PM To: Python Subject: Re: Which method to check if string index is queal to character. On Tue, Dec 29, 2020 at 10:08 AM Avi Gross via Python-list wrote: > > This may be a nit, but can we agree all valid email addresses as used > today have more than an @ symbol? > > I see it as requiring at least one character before the @ that come > from a list of allowed characters (perhaps not ASCII) but does not > include the symbol @ again. It is normally followed by some minimal > number of characters and maybe a period and one of the currently > valid domains like .com or .it but the latter gets tricky as it can > look like u...@abd.def.att.com or other long variations where only the > final component must be testable in the program. There can be an @ in the first part of the address, and the domain may well not have a dot. > The lack of an at-sign suggests it is not an email address. The lack > of anything before or after also seems to disqualify it. You may be > able to add more conditions but as noted, having more than one at-sign > may also disqualify it. Lack of an at sign means it's a local address that can't be routed over the internet, and in many contexts, it's reasonable to exclude those. But two isn't illegal. > I am sure someone has some complex regular expressions that they think > matches only potentially valid strings but, of course, as noted by > Chris, to really validate that an address works might require sending > something and validating a human replied and that can be quite task. > Yes, many such regexes exist, and they are *all wrong*. Without exception. I don't think it's actually possible for a regex to perfectly match all (syntactically) valid email addresses and nothing else. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-28, Mats Wichmann wrote: > On 12/28/20 10:46 AM, Marco Sulla wrote: >> On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: >>> >>> I'd like to check if there's "@" in a string and wondering if any method >>> is better/safer than others. I was told on one occasion that I should >>> use is than ==, so how would be on this example. >>> >>> s = 't...@mail.is' >> >> You could do simply >> >> if "@" in s: >> >> but probably what you really want is a regular expression. >> > > Will add that Yes, you should always validate your inputs, but No, the > presence of an @ sign in a text string is not sufficient to know it's a > valid email address. Unfortunately validating that is hard. > Nah, by saying if is valid I meant exeactly if there's "@", I could add yet if endswith() but at this point @ is enough. Yes the only possible way for full validation would be just sending email and waiting for reply. -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 12/28/20 8:02 PM, Chris Angelico wrote: > > Yes, many such regexes exist, and they are *all wrong*. Without > exception. I don't think it's actually possible for a regex to > perfectly match all (syntactically) valid email addresses and nothing > else. > > ChrisA Since Email addresses are allowed to have (comments) in them, and comments are allowed to nest, I think it takes something stronger than a regex to fully process them, but takes something that can handle a recursive grammar. I think that is the only thing that absolutely prevents using a regex (but not sure about it) but some of the rules will make things messy and need to use alternation and repetition. The one other thing that might block a regex is I think there is an upper limits to how long the address (or its parts) are allowed to be, and testing that at the same time as the other patterns is probably beyond what a regex could handle. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-28, Michael Torrie wrote: > On 12/28/20 10:46 AM, Marco Sulla wrote: >> On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: >>> >>> I'd like to check if there's "@" in a string and wondering if any method >>> is better/safer than others. I was told on one occasion that I should >>> use is than ==, so how would be on this example. >>> >>> s = 't...@mail.is' >> >> You could do simply >> >> if "@" in s: >> >> but probably what you really want is a regular expression. > > https://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/ > Interested article. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
Il 29/12/2020 02:48, Bischoop ha scritto: On 2020-12-28, Mats Wichmann wrote: On 12/28/20 10:46 AM, Marco Sulla wrote: On Mon, 28 Dec 2020 at 17:37, Bischoop wrote: I'd like to check if there's "@" in a string and wondering if any method is better/safer than others. I was told on one occasion that I should use is than ==, so how would be on this example. s = 't...@mail.is' You could do simply if "@" in s: but probably what you really want is a regular expression. Will add that Yes, you should always validate your inputs, but No, the presence of an @ sign in a text string is not sufficient to know it's a valid email address. Unfortunately validating that is hard. Nah, by saying if is valid I meant exeactly if there's "@", I could add yet if endswith() but at this point @ is enough. Yes the only possible way for full validation would be just sending email and waiting for reply. you could try this way: # --- from dns import resolver as dns emails=['john@fakeserver.bah', 'john@gmail.com'] for ue in emails: try: mxl = dns.resolve(ue.split('@')[1], 'MX') except: print(f'{ue}: bad mail server') else: if len(mxl) > 0: print(f'{ue}: valid mail server') # --- ... so, having verified the sever, you should only worry about the name. -- https://mail.python.org/mailman/listinfo/python-list
Re: Which method to check if string index is queal to character.
On 2020-12-29, jak wrote: > > you could try this way: > > # --- > from dns import resolver as dns > > emails=['john@fakeserver.bah', > 'john@gmail.com'] > > for ue in emails: > try: > mxl = dns.resolve(ue.split('@')[1], 'MX') > except: > print(f'{ue}: bad mail server') > else: > if len(mxl) > 0: > print(f'{ue}: valid mail server') > # --- > > ... so, having verified the sever, you should only worry about the name. I actually have tried, that's good idea. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list