Notepad++正则替换规则修改咨询:两处特定位置匹配替换需求
Hey there! Let's tackle your two regex requirements step by step—they're totally doable with some targeted lookaround assertions. Here's how to make it work:
First, we need to check if the 4th to 8th characters contain the substring "B490", then target the 10th character for replacement.
Regex Pattern & Replacement
- Find:
^(.{9})(?<=^.{3}.{0,4}B490)(.) - Replace:
$1[你的目标字符](replace[你的目标字符]with whatever character you want, e.g.,X)
How It Works
^(.{9}): Captures the first 9 characters (so we can keep them intact in the replacement).(?<=^.{3}.{0,4}B490): A positive lookbehind assertion that checks if "B490" exists within the 4th to 8th characters. Here's why:^.{3}skips the first 3 characters (landing us at the start of the 4th character)..{0,4}covers the range from the 4th to 8th character (0 to 4 characters after the first 3).
(.): Captures the 10th character, which we'll replace with your desired character.
Example
If your input string is ABCXB490Y12345, the 4th-8th characters are XB490 (contains "B490"). Replacing the 10th character (1) with X gives you ABCXB490YX2345.
For this requirement, we need to verify "FE45" exists in the 10th to 31st character range, then overwrite the 3rd-6th characters with "zzzz".
Regex Pattern & Replacement
- Find:
^(.{2})(?=.{7,21}FE45)(.{4})(.*) - Replace:
$1zzzz$3
How It Works
^(.{2}): Captures the first 2 characters to preserve them.(?=.{7,21}FE45): A positive lookahead assertion that checks for "FE45" in the 10th-31st character range:.{7}skips 7 characters after the first 2, landing us at the start of the 10th character..{21}ensures we don't go beyond the 31st character—this covers all valid starting positions for "FE45" within the target range.
(.{4}): Captures the 3rd-6th characters (the part we want to replace).(.*): Captures everything after the 6th character to keep it intact.
Example
If your input string is AB1234XXXXFE45YYYYYYYYYYYYYYYYYY, the 10th-31st characters include "FE45". Replacing the 3rd-6th characters (1234) with "zzzz" gives you ABzzzzXXXXFE45YYYYYYYYYYYYYYYYYY.
内容的提问来源于stack exchange,提问作者supertech123

