[Fixed] I was addicted to alphanumeric judgment of Python strings

*** Addendum *** There was something wrong with the article, so please read the additional part below the text.

I was addicted to

One day I was writing a web app on Google App Engine (Python).

There are times when you want to determine the characters that can be used in a password in a Web service, right?

When I looked it up, I heard that I could check if it was alphanumeric with string.isalnum (), so I used that.

But it's funny.

password = 'abc123'
password.isalnum()  #--> True

password = u'Ah'
password.isalnum()  #-->True eh?

Apparently, double-byte characters are judged to be True by the isalnum method. Terrible...

solution

I have no choice but to use regular expressions. By the way, I prepared various things.

import re


#Half-width lowercase letters
lowerReg = re.compile(r'^[a-z]+$')
def islower(s):
    return lowerReg.match(s) is not None

#Half-width capital letters
upperReg = re.compile(r'^[A-Z]+$')
def isupper(s):
    return upperReg.match(s) is not None

#Alphabetic characters
alphaReg = re.compile(r'^[a-zA-Z]+$')
def isalpha(s):
    return alphaReg.match(s) is not None

#Half-width numbers
digitReg = re.compile(r'^[0-9]+$')
def isdigit(s):
    return digitReg.match(s) is not None

#Half-width alphanumeric characters
alnumReg = re.compile(r'^[a-zA-Z0-9]+$')
def isalnum(s):
    return alnumReg.match(s) is not None

#Half-width alphanumeric characters or underscore
alnum_Reg = re.compile(r'^[a-zA-Z0-9_]+$')
def isalnum_(s):
    return alnum_Reg.match(s) is not None

#Half-width symbol
symbolReg = re.compile(r'^[!-/:-@[-`{-~]+$')
def issymbol(s):
    return symbolReg.match(s) is not None

#ASCII characters
asciiReg = re.compile(r'^[!-~]+$')
def isascii(s):
    return asciiReg.match(s) is not None



isalnum('abc123')  #--> True
isalnum(u'Ah')  #--> False

I felt that there was not enough information, so make a note of it.

Reference site

...

It doesn't seem to have anything to do with this, but Python3 isn't currently available on Google App Engine (up to Python 2.7). Therefore, there are two types of strings, Unicode and str (to be exact, byte strings), which is inconvenient ...

Postscript

I had you point out in the comment.

It seems that the isalnum method doesn't work for unicode strings. Encoding to UTF8 works as expected.

In addition to the isalnum method, you can use isalpha (alphabetic), isdigit (number), islower (lowercase), and isupper (uppercase).

There are no methods for other judgments (such as ASCII judgments), so use regular expressions in the body.

For Python 2.x

For str strings the isalnum method works correctly.

However, if you handle double-byte characters such as Japanese in the str character string, it is more convenient to use the unicode character string because you cannot get the number of characters as you want. In that case, convert it to str and check.

u'Ah'.encode('utf-8').isalnum()  #--> False

For Python 3.x

In the Python 3.x series, str and unicode are integrated, and although it is named str, it is actually unicode. Therefore, even ordinary string literals need to be encoded in utf-8.

'Ah'.encode('utf-8').isalnum()  #--> False

*** Added above ***

Recommended Posts

[Fixed] I was addicted to alphanumeric judgment of Python strings
[Python] I was addicted to not saving internal variables of lambda expressions
What I was addicted to when using Python tornado
What I was addicted to when migrating Processing users to Python
I was addicted to multiprocessing + psycopg2
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
I was addicted to confusing class variables and instance variables in Python
I was addicted to pip install mysqlclient
I was addicted to Flask on dotCloud
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to creating a Python venv environment with VS Code
Use Python from Java with Jython. I was also addicted to it.
The file name was bad in Python and I was addicted to import
[Introduction to json] No, I was addicted to it. .. .. ♬
I was able to recurse in Python: lambda
AtCoder AGC 041 C --I was addicted to the full search of Domino Quality
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I tried to summarize how to use matplotlib of python
A story that I was addicted to at np.where
I was able to repeat it in Python: lambda
[Python] I tried to get Json of squid ring 2
I was addicted to trying logging.getLogger in Flask 1.1.x
I want to manipulate strings in Kotlin like Python!
I tried to summarize the string operations of Python
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
I tried to find the entropy of the image with python
[IOS] GIF animation with Pythonista3. I was addicted to it.
I want to specify another version of Python with pyvenv
[Python] How to make a list of character strings character by character
I want to start a lot of processes from python
I want to know the features of Python and pip
I tried to implement blackjack of card game in Python
[Python3] List of sites that I referred to when I started Python
I was a little addicted to installing Python3.3 + mod_wsgi3.4 on Sakura VPS (CentOS), so a retrospective memo
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
A story that I was addicted to calling Lambda from AWS Lambda.
The record I was addicted to when putting MeCab on Heroku
I tried to make a regular expression of "amount" using Python
[Introduction to Python] I compared the naming conventions of C # and Python.
I tried to make a regular expression of "time" using Python
A note I was addicted to when making a beep on Linux
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
Note that I was addicted to sklearn's missing value interpolation (Imputer)
I tried to fix "I tried stochastic simulation of bingo game with Python"
I want to use both key and value of Python iterator
A note I was addicted to when creating a table with SQLAlchemy
Python> Comprehension> cells> I was taught how to use double comprehension / itertools
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
I'm addicted to Python 2D lists
I want to debug with Python