1

I want to print elements of list via regex this is my code:

myresult_tv = [ 'Extinct A Horizon Guide to Dinosaurs WEB h264-WEBTUBE', 'High Noon 2019 04 05 720p HDTV DD5 1 MPEG2-NTb', 'Wyatt Cenacs Problem Areas S02E01 1080p WEBRip x264-eSc', 'Bondi Vet S05E15 720p WEB x264-GIMINI', 'If Loving You Is Wrong S04E03 Randals Stage HDTV x264-CRiMSON', 'Wyatt Cenacs Problem Areas S02E01 WEBRip x264-eSc', 'Bondi Vet S05E15 1080p WEB x264-GIMINI']


li = []

for a in myresult_tv:
    w = re.match(".*\d ", a)
    c =w.group()
    li.append(c)

print(li)

and the result is :

    Traceback (most recent call last):
  File "azazzazazaaz.py", line 31, in <module>
    c =w.group()
AttributeError: 'NoneType' object has no attribute 'group'

***Repl Closed***
6
  • 1
    You need to check if (w): before accessing .group() on match object. What are you trying to extract from the string? Commented Apr 6, 2019 at 16:48
  • Why are you using regex if you're looking to get all elements from list? Commented Apr 6, 2019 at 16:49
  • @Austin no i want the get part of elements like High Noon 2019 04 05 but the first element make code rise error and i need to skip those element like first element make error Commented Apr 6, 2019 at 16:58
  • @CryNetPlan, this should be the question; not a comment. Commented Apr 6, 2019 at 16:59
  • @Austin yes i think the error explain this problem sorry , do you have any idea for skip the wrong element? Commented Apr 6, 2019 at 17:01

2 Answers 2

1

You're not checking if the regex matched the element of the list. You should be doing something like this:

match = re.search(pattern, string)
if match:
    process(match)
Sign up to request clarification or add additional context in comments.

Comments

0

Since I don't understand what your expected output, I use the same regex as yours. Try use this code:

li = []
for a in myresult_tv:
    try:                             # I use try... except... in case the regex doesn't work at some list elements
        w = re.search("(.*\d )", a)  # I use search instead of match
        c = w.group()
        li.append(c)
    except:
        pass

print(li)

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.