import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
Answer from CrazyCasta on Stack Overflowimport re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
One-liner: re.match(r"pattern", string) # No need to compile
import re
>>> if re.match(r"hello[0-9]+", 'hello1'):
... print('Yes')
...
Yes
You can evaluate it as bool if needed
>>> bool(re.match(r"hello[0-9]+", 'hello1'))
True
regex - Python: check if string meets specific format - Stack Overflow
String Format Checking in Python - Stack Overflow
python - Check if string has a certain format - Stack Overflow
regex - Python 2.6+ str.format() and regular expressions - Stack Overflow
Videos
As for the regex you're looking for I believe that
^bbbb[0-9a-f]{28}$
should validate correctly for your requirements.
As for if there is an easier way than using the re module, I would say that there isn't really to achieve the result you're looking for. While using the in keyword in python works in the way you would expect a contains method to work for a string, you are actually wanting to know if a string is in a correct format. As such the best solution, as it is relatively simple, is to use a regular expression, and thus use the re module.
In fact, Python does have an equivalent to the .contains() method. You can use the in operator:
if 'substring' in long_string:
return True
A similar question has already been answered here.
For your case, however, I'd still stick with regex as you're indeed trying to evaluate a certain String format. To ensure that your string only has hexadecimal values, i.e. 0-9 and a-f, the following regex should do it: ^[a-fA-F0-9]+$. The additional "complication" are the four 'b' at the start of your string. I think an easy fix would be to include them as follows: ^(bbbb)?[a-fA-F0-9]+$.
>>> import re
>>> pattern = re.compile('^(bbbb)?[a-fA-F0-9]+$')
>>> test_1 = 'bbbb00000000000000170d0000306fb6'
>>> test_2 = 'bbbb00000000000000170d0000306fx6'
>>> pattern.match(test_1)
<_sre.SRE_Match object; span=(0, 32), match='bbbb00000000000000170d0000306fb6'>
>>> pattern.match(test_2)
>>>
The part that is currently missing is checking for the exact length of the string for which you could either use the string length method or extend the regex -- but I'm sure you can take it from here :-)
If you are allowed to import re:
import re
u_id = input("Input User ID: ") #DECLARE u_id : string
rex = re.compile("^[A-Z][a-z]{2}[0-9]{3}$")
if rex.match(u_id):
print("Correct format")
else:
print("Incorrect")
Explanation of expression:
^represents the beginning of a string.[A-Z]is a range, containing all uppercase letters (in the English alphabet).[a-z]is a range, containing all lowercase letters.[0-9]is a range, containing all numbers.{n}specifies that n items (items are whatever is before the curly brackets) will be matched.$represents the end of the string.
Also, you can see more detailed explanations and test arbitrary strings against this regular expression here.
If you want to solve it without regular expressions (mind you, in this case they are the right tool!), you could do something like this:
id_format = [
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", # or string.ascii_uppercase etc.
"abcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnopqrstuvwxyz",
"0123456789",
"0123456789",
"0123456789",
]
def check(input):
# check for same length
if len(input) != len(id_format):
return False
for test, valid in zip(input, id_format): # itertools.zip_longest can make
if test not in valid: # the length check unnecessary
return False
return True
check("Abc123") # True
check("abc123") # False
This should do the trick. The regex string is ^[0-9]+\.[0-9]+\.[0-9]+$ Where I match each digit exactly 3 times, and check if a '.' separator is in the middle. ^ and $ signify the start and end of the string
>>> import re
>>> re.match('^[0-9]{3}\.[0-9]{3}\.[0-9]{3}$','111.222.333')
<_sre.SRE_Match object at 0x10f98cb28>
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','a11.22.33b')
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','1a1.22.3b3')
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','11.2a2.33')
This is definitely a job for regex. A simple regex for that pattern might be
\d\d\d\.\d\d\d\.\d\d\d
The "\d" stands for any digit, and the "\." is an escaped period character (because "." is a special symbol in regex.) With python re library would probably use the findall method with that pattern,
list_of_matches = re.findall("\d\d\d\.\d\d\d\.\d\d\d", my_string)
you first would need to format string and then use regex. It really doesn't worth it to put everything into a single line. Escaping is done by doubling the curly braces:
>>> pat= '^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}$'.format(domainName = 'delivery.com')
>>> pat
'^(w{3}\\.)?([0-9A-Za-z-]+\\.){1}delivery.com$'
>>> re.match(pat, str1)
Also, re.match is matching at the beginning of the string, you don't have to put ^ if you use re.match, you need ^ if you're using re.search, however.
Please note, that {1} in regex is rather redundant.
Per the documentation, if you need a literal { or } to survive the formatting opertation, use {{ and }} in the original string.
'^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}$'.format(domainName = 'delivery.com')
Hello all, this be my code:
zipCode = "AA 45522"
myPat1 = "(\d{1,9})([^,]+?)AA 45522" # the pattern i want
# now interpolating variable name into the regex pattern
myPat2 = f"(\d{1,9})([^,]+?){zipCode}"
myPat3 = rf"(\d{1,9})([^,]+?){zipCode}" #trying raw
myPat4 = "(\d{1,9})([^,]+?)"+zipCode
# Results I see in debugger:
myPat1 = '(\\d{1,9})([^,]+?)AA 45522' # correct
myPat4 = '(\\d{1,9})([^,]+?)AA 45522' # correct
myPat2 = '(\\d(1, 9))([^,]+?)AA 45522' # faulty
myPat3 = '(\\d(1, 9))([^,]+?)AA 45522' # faultymyPat2 & myPat3 are adding a space between (1, & 9) which is resulting into creation of faulty pattern (which in turn fails to find pattern in my rawString). So why does using f-string inside regex pattern fudge things up? I recall facing same problem few weeks back.
Finally I have to use myPat4 style of coding to make it work, but I love f-strings and want to find a proper solution to using f-strings inside regex pattern. Thanks.
Similar to Java. Use re.error exception:
import re
try:
re.compile('[')
is_valid = True
except re.error:
is_valid = False
exception
re.errorException raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.
Another fancy way to write the same answer:
import re
try:
print(bool(re.compile(input())))
except re.error:
print('False')
You can do it without using regex. Python has strptime() method which parses a string representing a time according to a format. If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.
Here is the code:
import time
given_time = "10:30:45"
try:
time.strptime(given_time, '%H:%M:%S')
print("Correct Format")
except ValueError:
print("Incorrect Format")
Here are its formats:
%a - abbreviated weekday name
%A - full weekday name
%b - abbreviated month name
%B - full month name
%c - preferred date and time representation
%C - century number (the year divided by 100, range 00 to 99)
%d - day of the month (01 to 31)
%D - same as %m/%d/%y
%e - day of the month (1 to 31)
%g - like %G, but without the century
%G - 4-digit year corresponding to the ISO week number (see %V).
%h - same as %b
%H - hour, using a 24-hour clock (00 to 23)
%I - hour, using a 12-hour clock (01 to 12)
%j - day of the year (001 to 366)
%m - month (01 to 12)
%M - minute
%n - newline character
%p - either am or pm according to the given time value
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
%U - week number of the current year, starting with the first Sunday as the first day of the first week
%V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
%W - week number of the current year, starting with the first Monday as the first day of the first week
%w - day of the week as a decimal, Sunday=0
%x - preferred date representation without the time
%X - preferred time representation without the date
%y - year without a century (range 00 to 99)
%Y - year including the century
%Z or %z - time zone or name or abbreviation
%% - a literal % character
You can use regex to match the format, then take action depending on if a match is found. Provide re.match() with a pattern and the string you want to check:
import re
strings = ["8:30:00", "16:00:00", "845:00", "aa:bb:00"]
for s in strings:
if re.match("\d{1,2}:\d{2}:\d{2}", s): # Will return True if pattern matches s
print("match: {}".format(s)) # Take action on a matching pattern
else:
print("no match: {}".format(s))
The pattern \d{1,2}:\d{2}:\d{2} will match 1 or 2 digits, colon, 2 digits, colon, and 2 digits
The above will print:
match: 8:30:00
match: 16:00:00
no match: 845:00
no match: aa:bb:00