Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python check if a string end with a number in a range valid

Tags:

python

regex

In my test code, I want to assert that a string ends with a number. Say the number is between [0,3):

assert_equals('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/number') #valid

assert_equals('/api_vod_asset/v0/assets/1', '/api_vod_asset/v0/assets/number') #valid

assert_equals('/api_vod_asset/v0/assets/5', '/api_vod_asset/v0/assets/number') #invalid

How to use regular expression or some other technique for number ?

like image 507
user3828398 Avatar asked Apr 28 '16 13:04

user3828398


People also ask

How do you check if a string ends with a number in Python?

Python String endswith() Method. Python String endswith() method returns True if a string ends with the given suffix, otherwise returns False.

How do you check if a value falls in a range Python?

You can check if a number is present or not present in a Python range() object. To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range.

How do you check if string ends with one of the strings from a list?

Use string. endswith() to check if a string ends with any string from a list. Call str. endswith(suffix) to check if str ends with suffix .

How do you check if a string contains an integer in Python?

We can use the isdigit() function to check if the string is an integer or not in Python. The isdigit() method returns True if all characters in a string are digits. Otherwise, it returns False.


4 Answers

You would probably want to use assertRegex:

test_case.assertRegex('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/[012]')

The one above works in the case of [0,3) range. If you do not want that restriction, you would likely want to have:

test_case.assertRegex('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/[\d]')

All the code above works after the following lines have been added to your snippet:

import unittest as ut
test_case = ut.TestCase()
like image 61
lev Avatar answered Oct 10 '22 23:10

lev


If it will always be at the same place in the string, you can use string.split

something like

def check_range(to_test, valid_range):
    return int(to_test.split('/')[-1]) in range(valid_range[0], valid_range[1] + 1)

then you can do

check_range('/api_vod_asset/v0/assets/0', [0,3]) #True
check_range('/api_vod_asset/v0/assets/1', [0,3]) #True
check_range('/api_vod_asset/v0/assets/5', [0,3]) #False
like image 36
nathan.medz Avatar answered Oct 11 '22 00:10

nathan.medz


First, use a regex like \d+$ to get any number (\d+) at the end of the string ($)...

>>> m = re.search(r"\d+$", s)

... then check whether you have a match and whether the number is in the required range:

>>> m is not None and 0 <= int(m.group()) < 3

Or use a range, if you prefer this notation (assuming upper bound of [0,3) is exclusive):

>>> m is not None and int(m.group()) in range(0, 3)
like image 2
tobias_k Avatar answered Oct 10 '22 22:10

tobias_k


I want to assert that a string ends with a number

if int(myString[-1]) in [0,1,2]:
     do something...
like image 1
alec_djinn Avatar answered Oct 10 '22 23:10

alec_djinn