Python Forum
Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att
#10
SOLUTION 1:


FIND:
b_content = re.search('^\s*<a href="(.*?)" title="View', new_file_content).group(1)
REPLACE:
old_file_content = re.sub(', in <a href="(.*?)" title="Vezi', f', in <a href="{b_content}" title="Vezi', old_file_content)


SOLUTION 2:

FIND:
b_content = re.match(r'^\s*<a href="(.*?)" title="View', new_file_content).group(1)
REPLACE:
old_file_content = re.sub(', in <a href="(.*?)" title="Vezi', f', in <a href="{b_content}" title="Vezi', old_file_content)


SOLUTION 3:

import re

b_content = re.match(r'^\s*<a href="(.*?)" title="View', new_file_content)
if b_content is not None:
    b_content = b_content.group(1)
else:
    b_content = "No match found"
SOLUTION 4:

import re

match = re.search('^\s*<a href="(.*?)" title="View', new_file_content)
if match is not None:
    b_content = match.group(1)
    old_file_content = re.sub(', in <a href="(.*?)" title="Vezi', f', in <a href="{b_content}" title="Vezi', old_file_content)
else:
    print("No match found")


SOLUTION 5: (use re.MULTILINE )

import re

match = re.search('^\s*<a href="(.*?)" title="View', new_file_content, re.MULTILINE)
if match is not None:
    b_content = match.group(1)
    old_file_content = re.sub(', in <a href="([^"]*)" title="Vezi', f', in <a href="{b_content}" title="Vezi', old_file_content)
else:
    print("No match found")
Reply


Messages In This Thread
RE: Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att - by Melcu54 - Jun-28-2023, 11:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'NoneType' re.search philnyland 1 345 Jan-20-2024, 03:24 AM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 820 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,142 Aug-24-2023, 05:14 PM
Last Post: snippsat
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,881 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 4,130 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,530 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  search file by regex SamLiu 1 957 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,918 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  Python Regex quest 2 2,443 Sep-22-2022, 03:15 AM
Last Post: quest
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,341 Sep-19-2022, 02:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020