简体   繁体   English

如何仅从输入中排除数字,标点符号? 蟒蛇

[英]How to exclude only numbers, punctuation marks from input? Python

Is there any way to make a rule, where answer to the input may be only String or string+numeric value without punctuation marks/space. 有没有办法制定规则,其中输入的答案可能只是字符串或字符串+数值,而没有标点符号/空格。 So that will look like. 这样看起来像。

Insert first name:  …
… name: 5363737 - **not allowed**
… name: Bird1 ! - **not allowed**
… name: Bird1 - **allowed**

Whatever you are "inserting" with might have its own validation functions, but if not, its a straightforward regular expression to filter with. 无论您“插入”什么,都可能具有其自己的验证功能,但如果没有,则可以使用其直接过滤的正则表达式。

import re

inserts = [
    'aBC123',
    'ABC 123',
    'i like pie.',
    '123abc',
]

for i in inserts:
    if re.match('^[a-zA-Z]+[0-9]*$', i):
#   if re.match('^[a-zA-Z0-9]*$', i):   #leaving my old error visible as a testament to the importance of reading the spec carefully.
        print('insert', i)
        # call insert function
    else:
        print('NOT inserting', i)
        # do whatever
    Code:

     or i in "5363737","Bird1 !", "Bird1", "Bird1923812", "Bird":

        mo = re.match(r'([A-Za-z]+[0-9]*)$',i)

        if mo:
            print("allowed {}".format(mo.group(0)))
        else:
            print("not allowed {}".format(i))

    not allowed 5363737
    not allowed Bird1 !
    allowed Bird1
    allowed Bird1923812
    allowed Bird

# if needed, as stated in the comment  char+number+char, number+char, 
# number+char+number.
for i in "536cat3737","Bird1 !", "Bird1", "Bird1923812bird", "Bird","777Bird":

    mo = re.match(r'([A-Za-z]*[0-9]+[A-Za-z]+|\d+[A-Za-z]+\d+)$',i)

    if mo:
        print("allowed {}".format(mo.group(0)))
    else:
        print("not allowed {}".format(i))

Without re module: 不带re模块:

from string import letters, digits
from itertools import chain

while True:
    user_input = input('Insert first name: ')
    if all(c in digits for c in user_input) or #invalid if only numbers   
       any(c not in chain(letters,digits) for c in user_input): #invalid if something else than letters+numbers
        print 'name: ' + user_input + '** not allowed **'
    else:
        print 'name: ' + user_input + '** allowed **'
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称必须仅排除数值和标点符号、空格。 PYTHON - Name must exclude only numeric values and punctuation marks, space. PYTHON 如何使用 .translate() 从 Python 3.x 中的字符串中删除标点符号? - How to remove punctuation marks from a string in Python 3.x using .translate()? 如何仅删除标点符号,例如“。” 和 ”,”? - How can I remove ONLY punctuation marks like "." and ","? 正则表达式:如何识别屏幕中的单词(或如何排除标点符号和数字) - Regex: how to identify words in a screen (or how to exclude punctuation and numbers) 如何在python中使用正则表达式反转标点符号? - How to reverse punctuation marks using regular expression in python? 正则表达式仅从文件中获取以字母开头的单词,并在 python 中删除仅包含数字和标点符号的单词 - Regular Expression to get only words from file starting with letter and removing words with only numbers and punctuation in python 在python中比较两个文件时如何忽略标点符号 - How to ignore punctuation marks when comparing two files in python 怎么给句子加标点符号? - How to add punctuation marks for the sentences? 完整的 Python 标点符号集(不仅仅是 ASCII) - Complete set of punctuation marks for Python (not just ASCII) 删除标点符号并返回有意义的句子-Python - Delete punctuation marks and return a meaningful sentence - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM