如何在多个子字符串的每一次出现后追加指定字符串?
Alright, let's tackle this problem. You've got a string stored in txt1 with those anchor and italic tags, and you need to append a specific string every time certain substrings show up. Here's how you can do this cleanly in Python, using regular expressions to target exactly what you need:
Option 1: Target Exact Known Substrings
If you have a fixed list of substrings you want to append to (like fcfs.cs and fcfs.cpp), this approach will find every occurrence of those substrings anywhere in the string and add your specified text:
import re # Your original string txt1 = '<a target="_blank" id="myIdoperatingsystemsrcfirstcomefirstservefcfscs" href="javascript:void(0)">1. fcfs.cs</a> <i id="myStaroperatingsystemsrcfirstcomefirstservefcfscs" class="fa fa-star"></i><br><a target="_blank" id="myIdoperatingsystemsrcfirstcomefirstservefcfscpp" href="javascript:void(0)">2. fcfs.cpp</a> <i id="myStaroperatingsystemsrcfirstcomefirstservefcfscpp" class="fa fa-star"></i><br><a target="_blank" id="myIdope...' # The string you want to append append_text = "?updated=2024" # List of substrings to target (expand this as needed) target_substrings = ["fcfs.cs", "fcfs.cpp"] # Build a regex pattern that matches any of the target substrings (escaped for safety) pattern = re.compile('|'.join(re.escape(sub) for sub in target_substrings)) # Replace each match with the substring + your append text modified_string = pattern.sub(lambda match: match.group() + append_text, txt1) # Print or use the modified string print(modified_string)
Key Notes:
re.escape()ensures any special characters in your target substrings (like dots) are treated as literal text, avoiding regex errors.- This will modify the substrings wherever they appear—whether in the
<a>tag text, the<i>tag IDs, or elsewhere.
Option 2: Target Substrings Only in <a> Tag Text
If you only want to append the string to the filenames inside the visible text of <a> tags (and leave the <i> tag IDs untouched), use a more specific regex to isolate those parts:
import re txt1 = '<a target="_blank" id="myIdoperatingsystemsrcfirstcomefirstservefcfscs" href="javascript:void(0)">1. fcfs.cs</a> <i id="myStaroperatingsystemsrcfirstcomefirstservefcfscs" class="fa fa-star"></i><br><a target="_blank" id="myIdoperatingsystemsrcfirstcomefirstservefcfscpp" href="javascript:void(0)">2. fcfs.cpp</a> <i id="myStaroperatingsystemsrcfirstcomefirstservefcfscpp" class="fa fa-star"></i><br><a target="_blank" id="myIdope...' append_text = "?v=1" # Regex to match the filename inside <a> tag text (e.g., "fcfs.cs" after "1. ") pattern = re.compile(r'(<a[^>]*>\d+\. )([a-z0-9]+\.(cs|cpp))(<\/a>)') # Replace by inserting the append text right after the filename modified_string = pattern.sub(r'\1\2' + append_text + r'\3', txt1) print(modified_string)
How This Works:
- The regex captures three groups: the start of the
<a>tag up to the number and space, the filename itself, and the closing</a>tag. - We reconstruct the tag with the append text added to the filename, leaving other parts of the string unchanged.
You can adjust the regex patterns or target substring list to fit your exact needs—just tweak the target_substrings or regex to match the specific parts of your string you want to modify.
内容的提问来源于stack exchange,提问作者Dimi Ansari

