15,049
edits
mNo edit summary |
|||
| (4 intermediate revisions 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|漢字]]}} | ||
{{Raise hand | text = '''Need Help?''' You can use the provided explanatory [[#regular-expression-online-tools|online tools]] to try debugging yourself. }} | |||
== Quick Reference Table == | == Quick Reference Table == | ||
| Line 121: | Line 121: | ||
Convert to: | Convert to: | ||
==== Method 1: Sublime Text, EmEditor ==== | ==== Method 1: Sublime Text, EmEditor ==== | ||
| Line 139: | Line 139: | ||
# Click “Replace All” | # Click “Replace All” | ||
==== Method 3: Microsoft Word ==== | ==== Method 3: Microsoft Word ==== | ||
| Line 148: | Line 148: | ||
# Click “Replace All” | # Click “Replace All” | ||
==== 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> | ||
=== 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> | ||
=== Remove Black Squares (UNIX Line Endings LF) === | === Remove Black Squares (UNIX Line Endings LF) === | ||
Using Notepad++: 1. Menu: Find -> 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 -> Replace 2. Search mode: Check “Extended mode” - Find: <code>\n\n</code> (2 LF characters) - Replace with: <code>\r\n</code> (CR and LF) | ||
=== Add Quotes Around Elements === | === Add Quotes Around Elements === | ||
==== 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>"$1"</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>"$1"</code> (for double quotes) | ||
=== Find Non-ASCII Characters (Chinese/Non-English Text) === | === Find Non-ASCII Characters (Chinese/Non-English Text) === | ||
==== In LibreOffice ==== | ==== In LibreOffice ==== | ||
<pre>[^\u0000-\u0080]+</pre> | <pre>[^\u0000-\u0080]+</pre> | ||
==== Find Chinese Characters in Google Sheets ==== | ==== Find Chinese Characters in Google Sheets ==== | ||
| Line 201: | Line 193: | ||
<pre>=IF(REGEXMATCH(A2, "[\一-\龥]"), "Chinese", "English")</pre> | <pre>=IF(REGEXMATCH(A2, "[\一-\龥]"), "Chinese", "English")</pre> | ||
==== Find Non-ASCII Characters in Google Sheets ==== | |||
Extract non-ASCII characters (such as Chinese, Japanese, emoji, etc.) from cell {{kbd | key=A2}} | Extract non-ASCII characters (such as Chinese, Japanese, emoji, etc.) from cell {{kbd | key=A2}} | ||
<pre> | <pre> | ||
| Line 242: | Line 234: | ||
FROM `table_name` | FROM `table_name` | ||
WHERE `column_name` <> CONVERT(`column_name` USING ASCII)</syntaxhighlight> | WHERE `column_name` <> CONVERT(`column_name` USING ASCII)</syntaxhighlight> | ||
==== Find Chinese Characters in PHP ==== | ==== Find Chinese Characters in PHP ==== | ||
| Line 273: | Line 265: | ||
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); | preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); | ||
var_dump($matches);</syntaxhighlight> | var_dump($matches);</syntaxhighlight> | ||
=== Find ASCII Characters in PHP === | === Find ASCII Characters in PHP === | ||
| Line 292: | Line 284: | ||
echo "Contains non-ASCII characters"; | echo "Contains non-ASCII characters"; | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
=== Remove Empty Lines === | === Remove Empty Lines === | ||
| Line 316: | Line 308: | ||
'''Using Notepad++ v7.8.7:''' - Menu: Edit -> Line Operations -> Remove Empty Lines (Including Blank Lines) | '''Using Notepad++ v7.8.7:''' - Menu: Edit -> Line Operations -> Remove Empty Lines (Including Blank Lines) | ||
=== Find Non-Whitespace Text === | === Find Non-Whitespace Text === | ||
* Find: <code>[^\s]+</code> | * Find: <code>[^\s]+</code> | ||
=== Convert Symbol-Separated Text to Line-by-Line Display === | === Convert Symbol-Separated Text to Line-by-Line Display === | ||
| Line 335: | 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> | ||
=== Replace Multiple Spaces with Tab Characters === | === Replace Multiple Spaces with Tab Characters === | ||
| Line 342: | 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> | ||
=== Remove Leading/Trailing Whitespace === | === Remove Leading/Trailing Whitespace === | ||
==== Remove Leading Whitespace ==== | ==== Remove Leading Whitespace ==== | ||
| Line 351: | Line 340: | ||
* Replace with: (empty) | * Replace with: (empty) | ||
==== Remove Trailing Whitespace ==== | ==== Remove Trailing Whitespace ==== | ||
| Line 357: | Line 346: | ||
* Replace with: (empty) | * Replace with: (empty) | ||
==== Remove Both Leading and Trailing Whitespace ==== | ==== Remove Both Leading and Trailing Whitespace ==== | ||
| Line 363: | Line 352: | ||
* Replace with: (empty) | * Replace with: (empty) | ||
== 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 | ||
== Syntax Reference == | == Syntax Reference == | ||
| Line 376: | 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 | ||
== 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>) | ||
== Alternative Solutions == | == Alternative Solutions == | ||
| Line 387: | Line 375: | ||
* Copy multiple rows and paste between different applications (compatibility varies) | * Copy multiple rows and paste between different applications (compatibility varies) | ||
== Further Reading == | == Further Reading == | ||
| Line 394: | Line 381: | ||
* Platform-specific regular expression documentation | * Platform-specific regular expression documentation | ||
{{Template:Data factory flow}} | {{Template: Data factory flow}} | ||
[[Category:Regular expression]] [[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:Search]] [[Category:String manipulation]] | [[Category: Regular expression]] | ||
[[Category: Software]] | |||
[[Category: Programming]] | |||
[[Category: Data Science]] | |||
[[Category: Search]] | |||
[[Category: String manipulation]] | |||
[[Category: Revised with LLMs] | |||