Regular expression: Difference between revisions

Jump to navigation Jump to search
1,056 bytes removed ,  11 December 2025
m
no edit summary
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
{{LanguageSwitcher | content = [[Regular expression | English]], [[Regular expression in Mandarin|漢字]]}}
{{LanguageSwitcher | content = [[Regular expression | English]], [[Regular expression in Mandarin|漢字]]}}


<blockquote>'''Need Help?''' You can use the provided explanatory [[#regular-expression-online-tools|online tools]] to try debugging yourself.
{{Raise hand | text = '''Need Help?''' You can use the provided explanatory [[#regular-expression-online-tools|online tools]] to try debugging yourself. }}
</blockquote>
 


== Quick Reference Table ==
== Quick Reference Table ==
Line 121: Line 121:
Convert to:
Convert to:
<span id="method-1-sublime-text-emeditor"></span>
 
==== Method 1: Sublime Text, EmEditor ====
==== Method 1: Sublime Text, EmEditor ====


Line 139: Line 139:
# Click “Replace All”
# Click “Replace All”


<span id="method-3-microsoft-word"></span>
 
==== Method 3: Microsoft Word ====
==== Method 3: Microsoft Word ====


Line 148: Line 148:
# Click “Replace All”
# Click “Replace All”


<span id="method-4-sed-command-for-linux"></span>
==== Method 4: Sed command for Linux ====
==== Method 4: Sed command for Linux ====


<syntaxhighlight lang="bash">sed ':a;N;$!ba;s/\n/; /g' old.filename > new.filename</syntaxhighlight>
<syntaxhighlight lang="bash">sed ':a;N;$!ba;s/\n/; /g' old.filename > new.filename</syntaxhighlight>
<span id="find-ip-addresses-ipv4"></span>
 
=== Find IP Addresses (IPv4) ===
=== Find IP Addresses (IPv4) ===


Line 159: Line 158:
For Sublime Text v. 3.2.21: - Find: <code>(?:\d{1,3}\.){3}\d{1,3}</code>
For Sublime Text v. 3.2.21: - Find: <code>(?:\d{1,3}\.){3}\d{1,3}</code>


<span id="remove-black-squares-unix-line-endings-lf"></span>
=== Remove Black Squares (UNIX Line Endings LF) ===
=== Remove Black Squares (UNIX Line Endings LF) ===


Using Notepad++: 1. Menu: Find -&gt; Replace 2. Search mode: Check “Extended mode” - Find: <code>\n\n</code> (2 LF characters) - Replace with: <code>\r\n</code> (CR and LF)
Using Notepad++: 1. Menu: Find -&gt; Replace 2. Search mode: Check “Extended mode” - Find: <code>\n\n</code> (2 LF characters) - Replace with: <code>\r\n</code> (CR and LF)


<span id="add-quotes-around-elements"></span>
=== Add Quotes Around Elements ===
=== Add Quotes Around Elements ===


<span id="add-quotes-around-array-elements"></span>
==== Add Quotes Around Array Elements ====
==== Add Quotes Around Array Elements ====


Line 184: Line 180:
'''Method 3: Notepad++''' (Enable “Regular expression” search mode) - Find: <code>([^\s|,]+)</code> - Replace with: <code>'$1'</code> (for single quotes) or <code>&quot;$1&quot;</code> (for double quotes)
'''Method 3: Notepad++''' (Enable “Regular expression” search mode) - Find: <code>([^\s|,]+)</code> - Replace with: <code>'$1'</code> (for single quotes) or <code>&quot;$1&quot;</code> (for double quotes)


<span id="find-non-ascii-characters-chinesenon-english-text"></span>
=== Find Non-ASCII Characters (Chinese/Non-English Text) ===
=== Find Non-ASCII Characters (Chinese/Non-English Text) ===


Line 289: Line 284:
     echo "Contains non-ASCII characters";
     echo "Contains non-ASCII characters";
}</syntaxhighlight>
}</syntaxhighlight>
<span id="remove-empty-lines"></span>
 
=== Remove Empty Lines ===
=== Remove Empty Lines ===


Line 313: Line 308:
'''Using Notepad++ v7.8.7:''' - Menu: Edit -&gt; Line Operations -&gt; Remove Empty Lines (Including Blank Lines)
'''Using Notepad++ v7.8.7:''' - Menu: Edit -&gt; Line Operations -&gt; Remove Empty Lines (Including Blank Lines)


<span id="find-non-whitespace-text"></span>
=== Find Non-Whitespace Text ===
=== Find Non-Whitespace Text ===


* Find: <code>[^\s]+</code>
* Find: <code>[^\s]+</code>


<span id="convert-symbol-separated-text-to-line-by-line-display"></span>
=== Convert Symbol-Separated Text to Line-by-Line Display ===
=== Convert Symbol-Separated Text to Line-by-Line Display ===


Line 332: Line 325:
'''Using Sublime Text or EmEditor:''' - Find: <code>([^、]+)([、]{1})</code> - Replace with: <code>\1\n</code>
'''Using Sublime Text or EmEditor:''' - Find: <code>([^、]+)([、]{1})</code> - Replace with: <code>\1\n</code>


<span id="replace-multiple-spaces-with-tab-characters"></span>
=== Replace Multiple Spaces with Tab Characters ===
=== Replace Multiple Spaces with Tab Characters ===


Line 339: Line 331:
'''Using Sublime Text:''' - Find: <code>([^\S\n]+)</code> or <code>([^\S\r\n]+)</code> or <code>\s\s+</code> - Replace with: <code>\t</code>
'''Using Sublime Text:''' - Find: <code>([^\S\n]+)</code> or <code>([^\S\r\n]+)</code> or <code>\s\s+</code> - Replace with: <code>\t</code>


<span id="remove-leadingtrailing-whitespace"></span>
 
=== Remove Leading/Trailing Whitespace ===
=== Remove Leading/Trailing Whitespace ===


<span id="remove-leading-whitespace"></span>
 
==== Remove Leading Whitespace ====
==== Remove Leading Whitespace ====


Line 348: Line 340:
* Replace with: (empty)
* Replace with: (empty)


<span id="remove-trailing-whitespace"></span>
 
==== Remove Trailing Whitespace ====
==== Remove Trailing Whitespace ====


Line 354: Line 346:
* Replace with: (empty)
* Replace with: (empty)


<span id="remove-both-leading-and-trailing-whitespace"></span>
 
==== Remove Both Leading and Trailing Whitespace ====
==== Remove Both Leading and Trailing Whitespace ====


Line 360: Line 352:
* Replace with: (empty)
* Replace with: (empty)


<span id="text-editors-supporting-regular-expressions"></span>
 
== Text Editors Supporting Regular Expressions ==
== Text Editors Supporting Regular Expressions ==


Various text editors support regular expressions including: - Sublime Text - EmEditor - Notepad++ - Visual Studio Code - Atom - Vim/Neovim
Various text editors support regular expressions including: - Sublime Text - EmEditor - Notepad++ - Visual Studio Code - Atom - Vim/Neovim


<span id="syntax-reference"></span>
 
== Syntax Reference ==
== Syntax Reference ==


Line 373: Line 365:
* Non-whitespace: <code>\S</code> - Does not include half-width spaces and full-width spaces
* Non-whitespace: <code>\S</code> - Does not include half-width spaces and full-width spaces


<span id="troubleshooting-regular-expressions"></span>
== Troubleshooting Regular Expressions ==
== Troubleshooting Regular Expressions ==


'''Tips:''' 1. Use online tools like regex101 to understand your syntax 2. Test with small data: Prepare small file data to verify syntax 3. Highlight or output matched text for debugging 4. Simplify the syntax when encountering issues 5. Try alternative syntax due to compatibility issues (e.g., <code>\d</code> to <code>[0-9]+</code>)
'''Tips:''' 1. Use online tools like regex101 to understand your syntax 2. Test with small data: Prepare small file data to verify syntax 3. Highlight or output matched text for debugging 4. Simplify the syntax when encountering issues 5. Try alternative syntax due to compatibility issues (e.g., <code>\d</code> to <code>[0-9]+</code>)


<span id="alternative-solutions"></span>
 
== Alternative Solutions ==
== Alternative Solutions ==


Line 384: Line 375:
* Copy multiple rows and paste between different applications (compatibility varies)
* Copy multiple rows and paste between different applications (compatibility varies)


<span id="further-reading"></span>
== Further Reading ==
== Further Reading ==


Line 391: Line 381:
* Platform-specific regular expression documentation
* Platform-specific regular expression documentation


{{Template:Data factory flow}}
{{Template: Data factory flow}}


[[Category: Regular expression]]  
[[Category: Regular expression]]  

Navigation menu