1

I have a list of strings that looks like this,

strlist = [
            'list/category/22',
            'list/category/22561',
            'list/category/3361b',
            'list/category/22?=1512',
            'list/category/216?=591jf1!',
            'list/other/1671',
            'list/1y9jj9/1yj32y',
            'list/category/91121/91251',
            'list/category/0027',
]

I want to use regex to find the strings in this list, that contain the following string /list/category/ followed by an integer of any length, but that's it, it cannot contain any letters or symbols after that.

So in my example, the output should look like this

list/category/22
list/category/22561
list/category/0027

I used the following code:

newlist = []
for i in strlist:
    if re.match('list/category/[0-9]+[0-9]',i):
        newlist.append(i)
        print(i)

but this is my output:

list/category/22
list/category/22561
list/category/3361b
list/category/22?=1512
list/category/216?=591jf1!
list/category/91121/91251
list/category/0027

How do I fix my regex? And also is there a way to do this in one line using a filter or match command instead of a for loop?

3
  • perfect!, can it be done in a single line? Commented May 26, 2020 at 6:17
  • i mean like instead of a for loop Commented May 26, 2020 at 6:19
  • You can use list comprehension if you want this done in a single line. Commented May 26, 2020 at 6:28

1 Answer 1

3

You can try the below regex:

^list\/category\/\d+$

Explanation of the above regex:

^ - Represents the start of the given test String.

\d+ - Matches digits that occur one or more times.

$ - Matches the end of the test string. This is the part your regex missed.

Demo of the above regex in here.

IMPLEMENTATION IN PYTHON

import re
pattern = re.compile(r"^list\/category\/\d+$", re.MULTILINE)
match = pattern.findall("list/category/22\n"
               "list/category/22561\n"
               "list/category/3361b\n"
               "list/category/22?=1512\n"
               "list/category/216?=591jf1!\n"
               "list/other/1671\n"
               "list/1y9jj9/1yj32y\n"
               "list/category/91121/91251\n"
               "list/category/0027") 
print (match)

You can find the sample run of the above implementation here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.