Python Checking Characters [duplicate] - python

This question already has answers here:
How do I verify that a string only contains letters, numbers, underscores and dashes? [duplicate]
(11 answers)
Closed 8 years ago.
I am trying to parse text and determine if it contains only letters and numbers, not special keyboard symbols like ! and #. I tried using .isalpha, but it says ! and # are valid. Is there away I can have something return false if it encounters one of these symbols?

Use regex matching:
import re
print re.match(r'^\w+$',your_string).group(0)
This matches the whole string only if it is alphanumeric
>>> print re.match(r'^\w+$', '1kjh2431k2j43').group(0)
'1kjh2431k2j43'
>>> print re.match(r'^\w+$', 'hjs7*Y##kha9Y*##').group(0)
NoneType

can check if the ordinals of the characters are in the ranges of characters, think the ranges are like 65-90ish and then 95-122ish, and then just check .isdigit() to see if the number was a digit beforehand

Related

Sub all the specified regex interval except some characters [duplicate]

This question already has answers here:
Exclude characters from a character class
(5 answers)
Closed 2 years ago.
For example, I want to replace all the data going from the specified intervals with * (except the chars u0650, u0660, u064F), for example.
Note: I don't want to break the interval because I have a lot of characters to preserve.
data = re.sub(r'[\u0600-\u061E\u0620-\u065F\u0670-\u06ef]', "*", data)
You can put the characters to be excluded in a negative Lookahead before the main character class.
For example:
(?![\u0650\u0660\u064F])[\u0600-\u061E\u0620-\u065F\u0670-\u06ef]
Demo.

python regex filter out exact string [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 3 years ago.
I'm trying to write a regex that filters out matches if they contain "plex" in them.
plex-release -> should not match
my-release -> should match
potato -> should match
Been playing with pythex and came up with this one that works partially:
(?![plex])(\w+)[-_](release|version)$
However this also messes with any other values containing the letter "p".
I'm trying to come up with a regex that leaves out matches that only contain the string "plex" and in this order, not just any letter from the string.
Yes, you can do it using this regex.
^((?!plex).)*$
Source : Regular expression to match a line that doesn't contain a word

In python, how do I regex match the "." char in, say, a string representing an address? [duplicate]

This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 3 years ago.
I am getting an address string from the client (first I check that it is a string under a certain length), and I have this so far: ^[a-zA-Z0-9 ]+$ but it will fail if the address has a "." char in it.
I want a single regex that also matches if a "." char shows up: the address can only contain spaces, numbers and letters.
Obviously, one can do if "." in address_string, but I am trying to do my check in one regex match.
How do I also escape/search for a "." char in a string?
Just put in an escape character:
^[a-zA-Z0-9 \.]+$

regex to detect strings which involves only (at least one) letter and special character [duplicate]

This question already has answers here:
Regex to validate passwords with characters restrictions
(3 answers)
Closed 6 years ago.
What might be a good regex to detect strings which involves only (at least one) letter and special character? I checked this, How to check if a string contains only [numbers OR special characters] in python, but it didn't help.
I want to detect strings which involves only (at least one) letter and special character in python.
maybe something like:
if re.search('\w+[!##$%*()]+', string) or re.search('[!##$%*()]+\w+', string):
do_something()

Removing characters from the left side of a Python string? [duplicate]

This question already has answers here:
How to remove the left part of a string?
(21 answers)
Closed 6 years ago.
How would I go about removing characters from the left side of a Python string? Example:
string = "Devicename MA:CA:DD:RE:SS"
What should I do to make a new string of just the MAC Address?
You can do different things:
string.split(' ')[1]
or
string[11:]
or
string[-14:]
both yielding
'MA:CA:DD:RE:SS'
The last option is the closest to what you want I suppose. It takes the leftmost 14 characters from the string.
Assuming that the string always has the format:
Devicename<space>MAC Address
You can do this by simply splitting the string on the space and taking the second element in the resulting list.
>>> string = "Devicename MA:CA:DD:RE:SS"
>>> string.split()[1]
'MA:CA:DD:RE:SS'
One note - I assume you know that is not a valid MAC address, correct?

Categories