简体   繁体   中英

Searching if the complete substring is in string not part of it

I have a problem where the 'in' operator is not searching for the complete substring of a string, but returns true if any consecutive character is in the string.

For example, for a given list defined graphic_pages_name_list ['118-SB710', '66kV FIH GIS','MCC800', 'MCC849'], i am searching all filenames in a folder and comparing if the contents of the list is part of the filename.

One such filename is '415V FI MCC849 Incomer'. The problem is that although MCC849 is in the list, 'MCC' is also in the list so there are cases where files become mixed up and duplicates are created etc.

In other words, the File_name is C:\temp\MCC800\415V FI MCC849 Incomer but i want it to be C:\temp\MCC849\415V FI MCC849 Incomer

I would like to search for the entire word, not just consecutive characters in the list.

Any help would be much appreciated.

Code below:

Page_name = '415V FI MCC849 Incomer' 
graphic_pages_name_list = ['118-SB710', '66kV FIH GIS','MCC800', 'MCC849']

for name in graphic_pages_name_list:
     path = ('C:\\temp\\' + name + '\\')
     if name in Page_name:
          File_name = (path + '%s' % (Page_name))

You can use regex word boundaries. I've joined it all into one big regex (\bstring\b)|... to test the string:

rex = re.compile("|".join(list(map(lambda x: "(\\b"+x+"\\b)", graphic_pages_name_list)))).match(Page_name)

Your code already produces the desired output:

>>> Page_name = '415V FI MCC849 Incomer'
>>> graphic_pages_name_list = ['118-SB710', '66kV FIH GIS','MCC800', 'MCC849']
>>>
>>> for name in graphic_pages_name_list:
...      path = ('C:\\temp\\' + name + '\\')
...      if name in Page_name:
...           File_name = (path + '%s' % (Page_name))
...
>>> File_name
'C:\\temp\\MCC849\\415V FI MCC849 Incomer'

Find the difference between your example code and the code you're actually running, and eliminate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM