<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Regular Expressions on Jsj&#39;s Blog</title>
    <link>https://hahamaker.top/tags/regular-expressions/</link>
    <description>Recent content in Regular Expressions on Jsj&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Fri, 03 May 2024 19:51:21 +0800</lastBuildDate>
    <atom:link href="https://hahamaker.top/tags/regular-expressions/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>regular expressions cheatsheet</title>
      <link>https://hahamaker.top/posts/regexp/</link>
      <pubDate>Fri, 03 May 2024 19:51:21 +0800</pubDate>
      <guid>https://hahamaker.top/posts/regexp/</guid>
      <description>&lt;h1 id=&#34;regular-expressions&#34;&gt;Regular Expressions&lt;/h1&gt;
&lt;h2 id=&#34;matching-table&#34;&gt;Matching Table&lt;/h2&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th style=&#34;text-align: center&#34;&gt;metacharacter&lt;/th&gt;
					&lt;th style=&#34;text-align: center&#34;&gt;matching things&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\d&lt;/code&gt; &lt;code&gt;\D&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;matching all the digits(0 to 9), matching any non-digit character&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;.(dot)&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;matching any single character(letter, digit, whitespace, everything)&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;matching a single letter inside &lt;code&gt;[]&lt;/code&gt;(square brackets)&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;[^]&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;matching any single character except for the letters inside &lt;code&gt;[]&lt;/code&gt;(square brackets) with the &lt;code&gt;^&lt;/code&gt;(hat)&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;[-]&lt;/code&gt; &lt;code&gt;[^-]&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;indicating a character range to match a single character&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\w&lt;/code&gt; &lt;code&gt;\W&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;this is equivalent to the character range &lt;code&gt;[A-Za-z0-9_]&lt;/code&gt;, matching any non-alphanumeric character&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;{}&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match repetition of character&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;*&lt;/code&gt; &lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match either &lt;code&gt;0 or more&lt;/code&gt; or &lt;code&gt;1 or more&lt;/code&gt; of the character that it follows(it always follows a character or group)&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;?&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;this metacharacter allows to match either 0 or 1 of the preceding character or group&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\s&lt;/code&gt; &lt;code&gt;\S&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match all whitespaces, matching any non-whitespace character&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;␣&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match space&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\t&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match tab&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\n&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match new line&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\r&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match carriage return(useful in Windows environments)&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;^&lt;/code&gt; &lt;code&gt;$&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;the start and the end of the line, &lt;strong&gt;it&amp;rsquo;s only used at the beginning or end of a line&lt;/strong&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;()&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;match groups&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;(())&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;nested groups&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;(|)&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to denote different possible sets of characters&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;&lt;code&gt;\b&lt;/code&gt;&lt;/td&gt;
					&lt;td style=&#34;text-align: center&#34;&gt;to match the boundary between a word and a non-word character&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&#34;details&#34;&gt;Details&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;\d&lt;/code&gt; the 123s&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;matching all the digits(0 to 9)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;.(dot)&lt;/code&gt; the dot&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;matching any single character(letter, digit, whitespace, everything). But when you want just to match a period, you need to escape the dot by using a slash &lt;code&gt;\.&lt;/code&gt; accordingly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;[]&lt;/code&gt; matching specific characters&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;matching a single letter inside square brackets&lt;/li&gt;
&lt;li&gt;e.g. the pattern &lt;code&gt;[abc]&lt;/code&gt; will only match a single a, b or c letter and nothing else.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;[^]&lt;/code&gt; excluding specific characters&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;matching any single character except for the letters inside &lt;code&gt;[]&lt;/code&gt;(square brackets) with the &lt;code&gt;^&lt;/code&gt;(hat)&lt;/li&gt;
&lt;li&gt;e.g. the pattern &lt;code&gt;[^abc]&lt;/code&gt; will match any single single character except for the letters a, b or c.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;[-]&lt;/code&gt; &lt;code&gt;[^-]&lt;/code&gt; character ranges&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;[0-6]&lt;/code&gt; will only match any single digit character from zero to six and nothing else.&lt;/li&gt;
&lt;li&gt;e.g. &lt;code&gt;[^n-p]&lt;/code&gt; will only match any single character except for letters n to p.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple character ranges can also be used in the same set of brackets, along with individual characters.&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;[A-Za-z0-9_]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;{}&lt;/code&gt; Catching some zzz&amp;rsquo;s&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;a{3}&lt;/code&gt; will match the &lt;code&gt;a&lt;/code&gt; character exactly three times.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;certain regular expression engines&lt;/strong&gt;(some engines may do not support) will even allow to specify a range for this repetition.
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;a{1,3}&lt;/code&gt; will match the &lt;code&gt;a&lt;/code&gt; character no more than 3 times, but no less than once and &lt;code&gt;a{1,}&lt;/code&gt; or &lt;code&gt;a{,3}&lt;/code&gt; also can be use.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;w{3}&lt;/code&gt;(three w&amp;rsquo;s), &lt;code&gt;[wxy]{5}&lt;/code&gt;(five characters, each of which can be a &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;x&lt;/code&gt; or &lt;code&gt;y&lt;/code&gt;) and &lt;code&gt;.{2,6}&lt;/code&gt;(between two and six of any character)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;*&lt;/code&gt; &lt;code&gt;+&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;\d*&lt;/code&gt; to match any number of digits(0 or more). &lt;code&gt;\d+&lt;/code&gt; to ensures that the input string has at least one digit.&lt;/li&gt;
&lt;li&gt;e.g. &lt;code&gt;a+&lt;/code&gt;(one or more a&amp;rsquo;s), &lt;code&gt;[abc]+&lt;/code&gt;(one or more of any a, b or c character) and &lt;code&gt;.*&lt;/code&gt;(zero or more of any character)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;?&lt;/code&gt; characters optional&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;ab?c&lt;/code&gt; will match either the strings &amp;ldquo;abc&amp;rdquo; or &amp;ldquo;ac&amp;rdquo; because the b is considered optional.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;To match a plain question mark character in a string, you will have to escape it using a slash &lt;code&gt;\?&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;\s&lt;/code&gt;(all whitespaces) &lt;code&gt;␣&lt;/code&gt;(space) &lt;code&gt;\t&lt;/code&gt;(tab) &lt;code&gt;\n&lt;/code&gt;(new line) &lt;code&gt;\r&lt;/code&gt;(carriage return(in Windows))&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;^&lt;/code&gt; &lt;code&gt;$&lt;/code&gt; the start and the end of the line&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. ^success will match only a line that begins with the word &amp;ldquo;success&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;()&lt;/code&gt; match groups&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;e.g. ^(IMG\d+.png)$ to capture and extract the full filename&lt;/li&gt;
&lt;li&gt;e.g. ^(IMG\d+).png$ will only capture the part before the period&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(())&lt;/code&gt; nested group&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;for example:
  Jan 1987    (Jan 1987) (1987)
  May 1969    (May 1969) (1969)
  Aug 2011    (Aug 2011) (2011)

^([A-Z][a-z]* (\d*))$
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(|)&lt;/code&gt; it&amp;rsquo;s all conditional&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Buy more (milk|bread|juice)&lt;/code&gt; to match only the strings &lt;code&gt;Buy more milk&lt;/code&gt;, &lt;code&gt;Buy more bread&lt;/code&gt; or &lt;code&gt;Buy more juice&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(?i)&lt;/code&gt; makes the regex case insensitive&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(?c)&lt;/code&gt; makes the regex case sensitive&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description>
    </item>
  </channel>
</rss>
