The var ‘srch_ptrn’ that I am using for a regex search pattern works fine in a python console, but in my Jupyter notebook, my use of the var on line 8 triggers “NameError: name ‘srch_ptrn’ is not defined”
I would appreciate any tips about what I am doing wrong, and how to do it right, or where to look for an answer.
The following code I am running (which I am indenting by 4 spaces, in the hope that that will make it appear as code):
import os
import sys
import re
src_file_dirs2reopen = r’E:\Apps\UtilitiesByMarc\Dirs2ReopenTheseDirsAfterReboot_for_Desktop_startup_aaa.cmd’
srch_ptrn = r’‘’(C:\WINDOWS\explorer.exe /e, “)(.+?)(?=”)(")‘’’
p = re.compile(srch_ptrn)
with open(src_file_dirs2reopen, encoding=‘utf8’) as f: #with open(src_file_dirs2reopen, encoding=‘utf8’) as f:
for line in f:
line = line.strip() #print(line)
#if re.match(r'(C:\\WINDOWS\\explorer\.exe /e, ")(.+?)(?=")(")', line):# This line runs fine in the Notebook
if re.match(srch_ptrn, line):#This line (line 8) triggers the error msg in the Notebook but not in a python console window
print(line)
else:
print('Error!!!. No match found.')
According to, “srch_ptrn” will be an object called pattern.
And it reduces to the object “static int pattern” or “static int *pattern” in C language.
The important thing here is that objects defined as static cannot be changed. However, objects defined as external variables in Python can be redefined in various ways.
Therefore, if you enter the regular expression pattern directly to that part, it will be resolved.