<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
    <title>bigfish的网络博客 - 正则表达式</title>
    <subtitle>记录技术和生活</subtitle>
    <link rel="self" type="application/atom+xml" href="https://blog.laofu.online/tags/zheng-ze-biao-da-shi/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blog.laofu.online"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2020-10-05T10:00:00+08:00</updated>
    <id>https://blog.laofu.online/tags/zheng-ze-biao-da-shi/atom.xml</id>
    <entry xml:lang="zh">
        <title>一个正则表达式导致 CPU 高的问题排查过程</title>
        <published>2020-10-05T10:00:00+08:00</published>
        <updated>2020-10-05T10:00:00+08:00</updated>
        
        <author>
          <name>bigfish</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.laofu.online/blog/regex-cpu/"/>
        <id>https://blog.laofu.online/blog/regex-cpu/</id>
        
        <summary type="html">&lt;p&gt;这篇文章记录一个正则表达是导致 CPU 高的问题排查。由于无法直接使用线上的代码测试，所以我自己把代码整理了下来，具体代码如下：&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-10&quot;&gt; AppMain&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt;	public&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt; static&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-6&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;String&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-10&quot;&gt; args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt; throws&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt; InterruptedException&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt;		final&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-3&quot;&gt; regex&lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;^([a-z0-9A-Z]+[-|&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-8&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-8&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;.)+[a-zA-Z]{2,}$&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt;		final&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-3&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;blog.laofu.online.fuweilao@vip.qq.com#&lt;/span&gt;&lt;span class=&quot;z-l-12 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-8&quot;&gt;		for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-3&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-2&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-2&quot;&gt; 1000000&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8&quot;&gt;			Matcher&lt;/span&gt;&lt;span class=&quot;z-l-3&quot;&gt; matcher&lt;/span&gt;&lt;span class=&quot;z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3&quot;&gt; RegexUtils&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-6&quot;&gt;matcher&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;regex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3&quot;&gt;			matcher&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-6&quot;&gt;find&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3&quot;&gt;			Thread&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-6&quot;&gt;sleep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-2&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-4&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-4&quot;&gt;			matcher.group();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;当运行程序的时候，我们可以看到 java 的进程占用了 CPU 了 &lt;code&gt;82.1%&lt;/code&gt;,由于我使用的服务器是 1核+2G, 所以 load avg 占用也很高。&lt;/p&gt;</summary>
        
    </entry>
</feed>
