Editing
Regular expression
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
When processing text files through regular expressions, you can quickly search for or replace strings that match specific rules. Processing is done on a line-by-line basis for string manipulation. Regular expressions are also known as regex, regexp, or pattern matching expressions. {{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 == Note: (1) Blue highlighted areas in samples represent text matching the rules, (2) The same text rule can have multiple representations {| class="wikitable" |- ! Text Rule ! Sample ! Opposite Text Rule ! Sample |- | Any single character (including spaces, but not newline) <br> <code>.</code> | <span style="background:#C6E3FF">W</span>hat Does the Fox Say? 12 狐狸怎叫 34 | | |- | Any character (including spaces), appears 1 or 0 times <br> <code>.?</code> = <code>.{0,1}</code> | <span style="background:#C6E3FF">W</span>hat Does the Fox Say? 12 狐狸怎叫 34 | | |- | Any number of multiple characters (including spaces) <br> <code>.*</code> = <code>.{0,}</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34</span> | | |- | Any number of characters (including spaces), at least 1 occurrence <br> <code>.+</code> = <code>.{1,}</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34</span> | | |- | Any number of spaces or newlines (at least 1 occurrence) <br> <code>\s+</code> | What<span style="background:#C6E3FF"> </span>Does the Fox Say? 12 狐狸怎叫 34 | Any number of characters (not including spaces or newlines) <br> <code>[^\s]+</code> = <code>[^\s]{1,}</code> = <code>[\S]+</code> = <code>[^ ]+</code> | <span style="background:#C6E3FF">What</span> Does the Fox Say? 12 狐狸怎叫 34 |- | Any number of ASCII characters (including English, numbers and spaces) <br> <code>[\x00-\x80]+</code> or <code>[[:ascii:]]+</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12</span> 狐狸怎叫 34 | Non-ASCII, i.e., Chinese characters appearing any number of times <br> <code>[^\x00-\x80]+</code> | What Does the Fox Say? 12 <span style="background:#C6E3FF">狐狸怎叫</span> 34 |- | Any number of uppercase/lowercase English letters, numbers and underscore (_) (not including spaces) <br> <code>[\w]+</code> = <code>[a-zA-Z0-9_]+</code> <br> PHP with <code>u</code> modifier supports Chinese characters | <span style="background:#C6E3FF">What</span> <span style="background:#C6E3FF">Does</span> <span style="background:#C6E3FF">the</span> <span style="background:#C6E3FF">Fox</span> <span style="background:#C6E3FF">Say</span>? <span style="background:#C6E3FF">12</span> 狐狸怎叫 <span style="background:#C6E3FF">_34</span> | Any number of characters that are not English letters, numbers and underscore (_) <br> <code>\W+</code> = <code>[^a-zA-Z0-9_]+</code> | |- | Any number of digits (not including spaces) <br> <code>[\d]+</code> = <code>[0-9]+</code> | What Does the Fox Say? <span style="background:#C6E3FF">12</span> 狐狸怎叫 34 | Any number of characters not including digits (including spaces) <br> <code>[^\d]+</code> = <code>[^0-9]+</code> = <code>\D+</code> | <span style="background:#C6E3FF">What Does the Fox Say? </span>12 狐狸怎叫 34 |- | Any number of Chinese characters <br> <code>[\p{Han}]+</code> | What Does the Fox Say? 12 <span style="background:#C6E3FF">狐狸怎叫</span> 34 | Any number of characters not including Chinese <br> <code>[^\p{Han}]+</code> | |- | Lines starting with “狐狸” <br> <code>^狐狸.*$</code> | <span style="background:#C6E3FF">狐狸怎叫 34 What Does the Fox Say?</span><br>柴犬怎叫 What Does the shiba inu say? | Lines not starting with “狐狸” <br> <code>^(?!狐狸).*$</code> | 狐狸怎叫 34 What Does the Fox Say?<br><span style="background:#C6E3FF">柴犬怎叫 What Does the shiba inu say?</span> |- | Lines ending with “怎叫” <br> <code>^.*怎叫$</code> | What Does the Fox Say? 12 狐狸怎叫 34<br><span style="background:#C6E3FF">What Does the shiba inu say? 柴犬怎叫</span> | Lines not ending with “怎叫” <br> <code>.*(?<!怎叫)$</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34</span><br>What Does the shiba inu say? 柴犬怎叫 |- | Lines containing “狐狸” <br> <code>^.*狐狸.*$</code> or <code>(狐狸)</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34</span><br>What Does the shiba inu say? 柴犬怎叫 | Lines not containing “狐狸” <br> <code>^((?!狐狸).)*$</code> | What Does the Fox Say? 12 狐狸怎叫 34<br><span style="background:#C6E3FF">What Does the shiba inu say? 柴犬怎叫</span> |- | Boolean logic AND: Lines containing both “狐狸” and “叫” <br> <code>(?=.*狐狸)(?=.*叫).*</code> or <code>狐狸.*叫\|叫.*狐狸</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34</span><br><span style="background:#C6E3FF">What Does the Fox Say? 12 不叫狐狸 34</span><br>What Does the shiba inu say? 柴犬怎叫 | | |- | Boolean logic OR: Lines containing “狐狸” or “叫” <br> <code>.*(狐狸\|叫).*</code> | <span style="background:#C6E3FF">What Does the Fox Say? 12 狐狸怎叫 34<br>What Does the shiba inu say? 柴犬怎叫</span><br>What Does the shiba inu say? 柴犬怎了 | Boolean logic: Lines not containing “狐狸” and not containing “柴犬” <br> <code>^((?!狐狸\|柴犬).)*$</code> | What Does the Fox Say? 12 狐狸怎叫 34<br>What Does the shiba inu say? 柴犬怎叫<br><span style="background:#C6E3FF">What Does the Husky say? 哈士奇怎叫</span> |- | Boolean logic NOT: Lines not containing “狐狸” but containing “柴犬” <br> <code>^((?!狐狸).)*(柴犬).*$</code> = <code>^(柴犬).*((?!狐狸).)*$</code> = <code>(柴犬).*((?!狐狸).)*</code> | What Does the Fox Say? 12 狐狸怎叫 34<br><span style="background:#C6E3FF">What Does the shiba inu say? 柴犬怎叫</span> | | |} == Regular Expression Online Tools == Websites for testing regular expression syntax: * {{Gd}} [http://regex101.com/ RegEx101] - “Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript” - Provides syntax explanations * {{Gd}} [http://gskinner.com/RegExr/ RegExr] - Learn, Build, & Test RegEx - Provides syntax explanations * [https://regexper.com/ Regexper] - Visual explanation of syntax using diagrams * [https://jex.im/regulex/ Regulex:JavaScript Regular Expression Visualizer] - JavaScript Regular Expression Visualizer - Visual explanation using diagrams * [http://www.rubular.com/ Rubular] - A Ruby regular expression editor and tester * [http://www.phpliveregex.com/ PHP Live Regex] * [http://www.regextester.com/ Regex Tester and Debugger Online] - JavaScript, PCRE, PHP == Common Use Cases == === Replace Newlines with Commas === Converting email lists into a format usable by email software: <pre>Original: aaa@email.com bbb@email.com ccc@email.com Convert to: aaa@email.com,bbb@email.com,ccc@email.com</pre> ==== Method 1: Sublime Text, EmEditor ==== # Menu: Search -> Replace # Check “Use Regular Expression” #* Find: <code>\n</code> (newline character) #* Replace with: <code>,</code> # Click “Replace all” ==== Method 2: Notepad++ ==== # Menu: Find -> Replace # Search mode: Check “Extended mode” (not “Regular expression”) #* Find: <code>\n</code> #* Replace with: <code>,</code> # Click “Replace All” ==== Method 3: Microsoft Word ==== # Menu: Edit -> Replace # Check extended mode #* Find: <code>^p</code> (paragraph mark) #* Replace with: <code>,</code> # Click “Replace All” ==== Method 4: Sed command for Linux ==== <syntaxhighlight lang="bash">sed ':a;N;$!ba;s/\n/; /g' old.filename > new.filename</syntaxhighlight> === Find IP Addresses (IPv4) === For Notepad++ v.5.9.5: - Find: <code>\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?</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) === 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 Array Elements ==== <pre>Before: Elmo, Emie, Granny Bird, Herry Monster, 喀喀獸 After: 'Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸'</pre> '''Method 1: PHP''' <syntaxhighlight lang="php">$users = array('Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸'); // Single quotes around each element $result = implode(",", preg_replace('/^(.*?)$/', "'$1'", $users)); // Double quotes around each element $result = implode(",", preg_replace('/^(.*?)$/', "\"$1\"", $users)); echo $result;</syntaxhighlight> '''Method 2: Sublime Text or EmEditor''' - 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) === ==== In LibreOffice ==== <pre>[^\u0000-\u0080]+</pre> ==== Find Chinese Characters in Google Sheets ==== Example: If cell {{kbd | key=A2}} contains any Chinese character, display “Chinese”, otherwise display “English”: <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}} <pre> =IF(ISERROR(REGEXEXTRACT(A2, "[^\x00-\x80]+")), "", REGEXEXTRACT(A2, "[^\x00-\x80]+")) </pre> Explanation of regular expression {{kbd | key=<nowiki>[^\x00-\x80]+</nowiki>}} * {{kbd | key=<nowiki>[\x00-\x80]</nowiki>}}: Represents the ASCII character range (character codes 0-128). (1) Standard ASCII range: 0-127 ({{kbd | key=<nowiki>0x00-0x7F</nowiki>}} aka * {{kbd | key=<nowiki>[\x00-\x7F]</nowiki>}})<ref>[https://www.commfront.com/pages/ascii-chart ASCII Chart – CommFront]</ref> (2) Character 128 (({{kbd | key=<nowiki>0x80</nowiki>}}) is actually the first character in the extended ASCII range, not part of the original ASCII standard.<ref>[https://en.wikipedia.org/wiki/UTF-8 UTF-8 - Wikipedia]</ref><ref>[https://en.wikipedia.org/wiki/Control_character Control character - Wikipedia]</ref> * {{kbd | key=<nowiki>[^...]</nowiki>}}: Means "not" these characters * {{kbd | key=<nowiki>+</nowiki>}}: Means one or more Overall meaning: Matches one or more non-ASCII characters ==== Find Chinese Characters in MySQL ==== Find rows where <code>column_name</code> contains Chinese characters: <pre lang="sql">SELECT `column_name` FROM `table_name` WHERE HEX(`column_name`) REGEXP '^(..)*(E[4-9])';</pre> Query condition used to match records where the <code>column_name</code> field contains only Chinese characters. <pre lang="sql">SELECT `column_name` FROM `table_name` WHERE `column_name` REGEXP '^[一-龯]+$';</pre> Explanation: * {{kbd | key=<nowiki>[一-龯]</nowiki>}} - Character set that matches all characters from "一" to "龯" in Unicode * "一" has Unicode code point {{kbd | key=<nowiki>U+4E00</nowiki>}}<ref>[https://www.compart.com/en/unicode/U+4E00 “一” U+4E00 CJK Unified Ideograph-4E00 Unicode Character]</ref> * "龯" has Unicode code point {{kbd | key=<nowiki>U+9FEF</nowiki>}}<ref>[https://www.compart.com/en/unicode/U+9FAF “龯” U+9FAF CJK Unified Ideograph-9FAF Unicode Character]</ref> * This range U+4E00-U+9FFF already covers over 99% of daily Chinese usage requirements [https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_B Extension B] and later blocks mainly contain ancient Chinese characters, variant characters, etc., which rarely appear in modern texts ==== Find Non-ASCII Characters in MySQL ==== Find rows where <code>column_name</code> is not entirely ASCII characters: <syntaxhighlight lang="sql">SELECT `column_name` FROM `table_name` WHERE `column_name` <> CONVERT(`column_name` USING ASCII)</syntaxhighlight> ==== Find Chinese Characters in PHP ==== '''Exact match:''' <syntaxhighlight lang="php">// Approach 1 if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string)) { echo "All text is Chinese characters" . PHP_EOL; } else { echo "Some text is not Chinese characters" . PHP_EOL; } // Approach 2 if (preg_match('/^[\p{Han}]+$/u', $string)) { echo "All text is Chinese characters" . PHP_EOL; } else { echo "Some text is not Chinese characters" . PHP_EOL; }</syntaxhighlight> '''Partial match:''' <syntaxhighlight lang="php">// Approach 1 $string = '繁體中文-简体中文-English-12345-。,!-.,!-⭐'; $pattern = '/[\p{Han}]+/u'; preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); var_dump($matches); // Approach 2 $string = '繁體中文-简体中文-English-12345-。,!-.,!-⭐'; $pattern = '/[\x{4e00}-\x{9fa5}]+/u'; preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); var_dump($matches);</syntaxhighlight> === Find ASCII Characters in PHP === '''Code I:''' <syntaxhighlight lang="php">if (preg_match('/[^\x20-\x7f]/', $keyword) === 0) { echo "The keyword is ASCII only"; } else { echo "The keyword contains non-ASCII characters (like Chinese, Japanese, etc.)"; }</syntaxhighlight> '''Code II:''' <syntaxhighlight lang="php">$pattern = '/^[[:ascii:]]+$/i'; $text = "Hello World"; // ASCII only if (preg_match($pattern, $text)) { echo "Pure ASCII characters"; } else { echo "Contains non-ASCII characters"; }</syntaxhighlight> === Remove Empty Lines === '''Original:''' <pre>Neo Trinity Morpheus Smith Oracle</pre> '''After:''' <pre>Neo Trinity Morpheus Smith Oracle</pre> '''Using Sublime Text & EmEditor:''' - Find: <code>^[\s\t]*$\n</code> - Replace with: (empty) '''Using Notepad++ v7.8.7:''' - Menu: Edit -> Line Operations -> Remove Empty Lines (Including Blank Lines) === Find Non-Whitespace Text === * Find: <code>[^\s]+</code> === Convert Symbol-Separated Text to Line-by-Line Display === '''Example:''' <pre>Before: 尼歐、莫斐斯、崔妮蒂、史密斯、祭師 After: 尼歐 莫斐斯 崔妮蒂 史密斯 祭師</pre> '''Using Sublime Text or EmEditor:''' - Find: <code>([^、]+)([、]{1})</code> - Replace with: <code>\1\n</code> === Replace Multiple Spaces with Tab Characters === '''Before:''' <code>aaa bbb ccc</code> '''After:''' <code>aaa\tbbb\tccc</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 Whitespace ==== * Find: <code>^\s+</code> * Replace with: (empty) ==== Remove Trailing Whitespace ==== * Find: <code>\s+$</code> * Replace with: (empty) ==== Remove Both Leading and Trailing Whitespace ==== * Find: <code>(^\s+|\s+$)</code> * Replace with: (empty) == Text Editors Supporting Regular Expressions == Various text editors support regular expressions including: - Sublime Text - EmEditor - Notepad++ - Visual Studio Code - Atom - Vim/Neovim == Syntax Reference == * Newline character: <code>\r\n</code> (for Notepad++: Extended mode & Regular expression mode) * Tab character: <code>\t</code> (for Notepad++: Extended mode) * Digits: <code>\d</code> (for Notepad++: Regular expression mode only) * Non-whitespace: <code>\S</code> - Does not include half-width spaces and full-width spaces == 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>) == Alternative Solutions == * Use Tab-separated data that can be easily pasted into Google Sheets or MS Excel * Copy multiple rows and paste between different applications (compatibility varies) == Further Reading == * Regular-Expressions.info - Regex Tutorial, Examples and Reference * Unicode character properties documentation * Platform-specific regular expression documentation {{Template: Data factory flow}} [[Category: Regular expression]] [[Category: Software]] [[Category: Programming]] [[Category: Data Science]] [[Category: Search]] [[Category: String manipulation]] [[Category: Revised with LLMs]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Data factory flow
(
edit
)
Template:Gd
(
edit
)
Template:Kbd
(
edit
)
Template:LanguageSwitcher
(
edit
)
Template:Raise hand
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information