Saroekin
Saroekin

Reputation: 1245

How to address/check whether a character in a string isn't a disallowed character?

To be more specific, if I have a string as such:

string = "app-_l#e"

Now, all letter, numerical, hyphens, and underscores are allowed; though any other character is not.

Is there a function that would filter out designated types and/or single characters?

Upvotes: 1

Views: 205

Answers (2)

Bhargav Rao
Bhargav Rao

Reputation: 52151

You can use re, However note that it will be slower.

>>> import re
>>> pat = re.compile(r'[\d|\w|\-|_]+$')
>>> if pat.match("app-_l#e"):
...     print True
... else:
...     print False
... 
False

And a matching example

>>> if pat.match("apple123_-"):
...     print True
... else:
...     print False
... 
True

Upvotes: 3

Padraic Cunningham
Padraic Cunningham

Reputation: 180471

You can use str.translate to remove the allowed chars from the string leaving you with an empty string if only allowed chars are in the string or else a string of forbidden chars:

from string import ascii_letters, digits

s = "app-_l#e"
# if the string is empty
if not s.translate(None, "-_" + ascii_letters + digits):
    # string only contains allowed characters

After translating your input string would look like:

In [7]: from string import ascii_letters, digits

In [8]: s = "app-_l#e"

In [9]: s.translate(None, "-_" + ascii_letters + digits)
Out[9]: '#'

Or keep a set of allowed chars:

from string import ascii_letters, digits
allowed = set("-_" + ascii_letters + digits)
s = "app-_l#e"
if all(ch in allowed for ch in s):
     # string only contains allowed characters

Of you want to find out what forbidden characters are in the string either print s.translate(None, "-_" + ascii_letters + digits) which will only have any chars not allowed or iterate in a for loop:

from string import ascii_letters, digits
allowed = set("-_" + ascii_letters + digits)
s = "app-_l#e"
disallowed = [ch for ch in s if ch not in allowed] 

Upvotes: 3

Related Questions