Notepad++文本处理需求:将末尾字母移至开头
Got it, let's solve this problem for you! You need to take strings that start with numbers and end with 1-2 letters, then move those trailing letters to the front. Here are a couple of straightforward, practical ways to get this done:
Python Script Solution
If you prefer a reusable script that works across operating systems, this Python approach will let you process your text file easily:
import re # Regex pattern to capture digits (first group) and trailing 1-2 letters (second group) pattern = re.compile(r'(\d+)([A-Za-z]{1,2})$') # Read input file, process each line, then write output with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile: for line in infile: # Split line into individual elements elements = line.strip().split() processed = [] for elem in elements: match = pattern.match(elem) if match: # Swap letters and digits processed.append(f"{match.group(2)}{match.group(1)}") else: # Keep elements that don't match the pattern unchanged processed.append(elem) # Write the processed line back outfile.write(' '.join(processed) + '\n')
How it works:
- The regex
(\d+)([A-Za-z]{1,2})$targets exactly what you need: one or more digits, followed by 1-2 letters at the end of the string. - For each matching element, we swap the captured letters and digits, then reassemble the line.
Terminal (sed) Solution
If you're on Linux/macOS and want a quick command-line fix, use sed with regex substitution:
sed -E 's/([0-9]+)([A-Za-z]{1,2})/\2\1/g' input.txt > output.txt
How it works:
- The
s/old/new/gsyntax tellssedto replace all matches in each line. ([0-9]+)captures the leading digits,([A-Za-z]{1,2})captures the trailing letters.\2\1swaps the order of the two captured groups, putting letters first.
Test with Your Sample
For your input:
56465A 498558DF 90255C
Both methods will output exactly what you want:
A56465 DF498558 C90255
内容的提问来源于stack exchange,提问作者Shadi
相关产品推荐
相关产品推荐

