Notepad++正则表达式:为方括号内数字开头内容添加引号
Got it, let's fix that regex issue in Notepad++! The problem with your original pattern is that the greedy .* is matching way more than you want—if there are multiple square brackets on a line, it'll grab everything from the first [ to the last ] instead of stopping at the nearest closing bracket. Plus, we can tighten up the pattern to only target square-bracket wrapped names that start with a digit, which is exactly what you need.
Step 1: Use the Correct Regex Pattern
In Notepad++, open the Replace dialog (Ctrl+H):
- Find what:
\[(\d.*?)\] - Replace with:
["$1"] - Make sure the "Regular expression" radio button is selected, and ". matches newline" is unchecked (we don't want to cross lines accidentally).
Why This Works:
\[: Matches the opening square bracket literally.(\d.*?): Captures the content inside the brackets, where:\d: Ensures the first character is a digit (so we only target column names starting with numbers)..*?: Non-greedy match for any characters after the digit, stopping at the first closing bracket (avoids over-matching multiple brackets on the same line).
\]: Matches the closing square bracket literally.- The replacement
["$1"]wraps the captured column name in quotes while keeping the original square brackets intact.
Testing with Your Sample SQL
Original line:
SELECT HAZARD.[LOAN_NO],HAZARD.[FIRST_PRIN_BAL],HAZARD.[352_Ins_Co_Code] from HAZARD where (((HAZARD.[352_Ins_Co_Code]) = [PAYEE_CODE] ));
After replacement:
SELECT HAZARD.[LOAN_NO],HAZARD.[FIRST_PRIN_BAL],HAZARD.["352_Ins_Co_Code"] from HAZARD where (((HAZARD.["352_Ins_Co_Code"]) = [PAYEE_CODE] ));
Perfect—only the digit-starting column names get quoted, and non-digit ones like [LOAN_NO] and [PAYEE_CODE] stay untouched.
Alternative Pattern (If You Prefer Your Original Structure)
If you want to stick to capturing the brackets separately like your initial attempt, use this instead:
- Find what:
(\[)(\d.*?)(\]) - Replace with:
\1"\2"\3
This does the exact same thing, just splits the opening bracket, column name, and closing bracket into separate capture groups for clarity.
内容的提问来源于stack exchange,提问作者C0ppert0p

