1

I would like to allow these characters [a-z]+\.+[0-9]*\_* (Must contain one or more lowercase alphabetical characters(a-z) and Must contain one or more periods(.) also can contain zero or more digits(0-9), zero or more underscores(_)) , but no others.

I have tried multiple ways without success:

import re
iStrings = str(input('Enter string? '))
iMatch = re.findall(r'[a-z]+\.+[0-9]*\_*', iStrings)
iiMatch = re.findall(r'[~`!@$%^&*()-+={}\[]|\;:\'"<,>.?/]', iStrings)
iiiMatch = iMatch != iiMatch
if iiiMatch:
    print(':)')
else:
    print(':(')

Another example:

import re
iStrings = str(input('Enter string? '))
iMatch = re.findall(r'[a-z]+\.+[0-9]*\_*', iStrings) not "[~`!@$%^&*()-+={}\[]|\;:\'"<,>.?/]" in iStrings 
if iMatch:
   print(':)')
else:
   print(':(')

Any help would be much appreciated.

Edit: added clarification.

Edit: For additional information: https://forums.uberent.com/threads/beta-mod-changes.51520/page-8#post-939265

loki_999
  • 13
  • 3
  • FYI added Python code to the answer. – zx81 Jun 08 '14 at 22:56
  • For a more generalized take on input validation, (to which this question is a simplified example of), take a look at some (of the _many_) password validatiuon questions and answers: e.g. [regex for password](http://stackoverflow.com/a/9611715/433790) – ridgerunner Jun 08 '14 at 23:22
  • @ridgerunner nice link :) – zx81 Jun 09 '14 at 00:02

1 Answers1

1
allow these characters [a-z]+\.+[0-9]*\_*

First off, [a-z]+ is not "a" character. Neither is [0-9]* nor \_*

I am assuming that you mean you want to allow letters, digits, underscores, dots, plusses and asterisks.

Try this:

^[\w*.+]+$
  1. The \w already matches [a-z], [0-9] and _
  2. The anchors ^ and $ ensure that nothing else is allowed.
  3. From your question I wasn't clear if you wanted to allow a + character to match. If not, remove it from the character class: ^[\w*.]+$. Likewise, remove the * if it isn't needed.

In code:

if re.search(r"^[\w*.+]+$", subject):
    # Successful match
else:
    # Match attempt failed

EDIT following your comment:

For a string that must contain one or more letter, AND one or more dot, AND zero or more _, AND zero or more digits, we need lookaheads to enforce the one or more conditions. You can use this:

^(?=.*[a-z])(?=.*\.)[\w_.]+$

zx81
  • 41,100
  • 9
  • 89
  • 105
  • Sorry about that wasn't clear. Lower case characters `[a-z]` one or more, one or more `.`, zero or more `_` and zero more `[0-9]`. it's my first time using regex and man is it confusion. Thank you. – loki_999 Jun 08 '14 at 23:26
  • @loki_999 Thanks for your feedback. Are you saying that the expression must contain one or more letter, AND one or more dot, AND zero or more _, AND zero or more digits? Then that's a different regex `^(?=.*[a-z])(?=.*\.)[\w_.]+$` (we need to use lookaheads for validation) – zx81 Jun 08 '14 at 23:43
  • Exactly. https://forums.uberent.com/threads/beta-mod-changes.51520/page-8#post-939265 – loki_999 Jun 09 '14 at 00:04
  • @loki_999 Well that's alright then since I added that to the answer as well. :) – zx81 Jun 09 '14 at 00:06