<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]Ansible里的后向引用124进行替换,以及正则匹配shell下写在一行和写YAML文件的正则区别，结合正则猫RegexBuddy/Patterns的正确用法。PHP正则匹配反斜杠和美元$的方法以及Ansible单行shell交互和写入yml文件的正则不同写法的原因和对比成功实践及理解。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 27 Jun 2018 03:23:41 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	方便后面类似的需求，更快替换及测试，提高效率，如下：<br/><textarea name="code" class="php" rows="15" cols="100">
---
- name: easyswoole去掉PHP的 symlink和 stream_socket_server限制
&nbsp;&nbsp;gather_facts: no
&nbsp;&nbsp;hosts: &quot;&#123;&#123; h &#124; default(&#039;10.244.25.77&#039;) &#125;&#125;&quot;
&nbsp;&nbsp;tags: aixiu_web
&nbsp;&nbsp;tasks:
&nbsp;&nbsp;- name: easyswoole去掉PHP的 symlink和 stream_socket_server限制
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /usr/local/php/etc/php.ini
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: &#039;(.*)(symlink,)(.*)(stream_socket_server,)(.*)&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: &#039;&#92;1&#92;3&#92;5&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes
&nbsp;&nbsp;&nbsp;&nbsp;tags: getridofphpdisablefun
</textarea><br/>ansible-playbook replace.yml -C -D<br/><br/><br/><br/>前置之1)Ansible的正则替换的模拟替换参数 -C -D,类似sed 的 -n 和 p结合只显示不真实替换，如下：<br/>ansible-playbook aixiu_web.yml -e h=10.244.5.108 -C -D -t addhttpcdnsrcip<br/><br/>前置之2)正则被多重括号包起来的一个顺序和内容界定相当重要，它是从左到右数的一个&#92;1&#92;2&#92;3的反向引用实践备忘：<br/><textarea name="code" class="php" rows="15" cols="100">
regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
line: &#039;&#92;1--&#92;2--&#92;3--&#92;4&#124;&#124;&#039;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#line: &#039;&#92;1&#92;n&#92;2&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;&#92;4&#039;

-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;--&quot;agent&quot;:&quot;$http_user_agent&quot;--,&#039;&#124;&#124;
</textarea><br/>上面1是最左边那个（，也就是所有的，第二个是匹配到的&#039;，它前面还有一些空行也被匹配上了的（&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;），所以上面显示有一些空的主要用来对新加的一行对齐，<br/>第三个&#92;3就是上一行去掉前和后的部分对于插入这行没有用（前无&#039;后无,&#039;）(就是：&quot;agent&quot;:&quot;$http_user_agent&quot;)，第四个就是匹配到单独的一个（,&#039;）,<br/>这个道理明白了也就对正则匹配出来的先后顺序有一个了解，再就是这个串特比有是&#92;2里面的多个空格加一个单引号（&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;），<br/>对于&#92;n后面新加的一行对齐很重要，再就是&#92;4也就是（,&#039;）,也是用来补充新加的一行少的部分,组成新一行起到了作用，见实践2：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;=== &#92;2<br/>&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;&nbsp;&nbsp;&nbsp;&nbsp; &lt;===新加的部分<br/>,&#039;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;=== &#92;4&nbsp;&nbsp;&nbsp;&nbsp; <br/>上面三行构成了一个完整的和上面一样有N个空格打头的字符串：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/><br/>backrefs参数：默认情况下，当根据正则替换文本时，即使regexp参数中的正则存在分组，在line参数中也不能对正则中的分组进行引用，除非将backrefs参数的值设置为yes。backrefs=yes表示开启后向引用，这样，line参数中就能对regexp参数中的分组进行后向引用了，这样说不太容易明白，可以参考后面的示例命令理解。backrefs=yes除了能够开启后向引用功能，还有另一个作用，默认情况下，当使用正则表达式替换对应行时，如果正则没有匹配到任何的行，那么line对应的内容会被插入到文本的末尾，不过，如果使用了backrefs=yes，情况就不一样了，当使用正则表达式替换对应行时，同时设置了backrefs=yes，那么当正则没有匹配到任何的行时，则不会对文件进行任何操作，相当于保持原文件不变。<br/><br/>原文：https://blog.csdn.net/dylloveyou/article/details/80698531 <br/><br/>实践1）一行命令实现了正则替换，也就是Shell-&gt;Ansible的一个路径，它于直接定到yml里的正则写法不一样，看实践2作比较，原因：<br/>写到文件里和一行的正则写法是不一样的，写一行涉及到终端传Shell的问题，而写到yaml的Ansible文件里则是python的交互，所以正则写法不大一样，如下：<br/>shell交互：&nbsp;&nbsp;regexp=&#039;((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;&#92;&#92;$http_user_agent&#92;&quot;)(.*))&#039; <br/>yaml交互：&nbsp;&nbsp;regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;<br/>比对发现：<br/>一）&#92;: 冒号在shell不需要转义，而在yaml文件里需要转义。<br/>二）而shell里对$转义的右斜杠需要再加两个右斜杠一共三次，而yaml文件里轩一次也就行了,后面有描述这个$的问题。<br/><textarea name="code" class="php" rows="15" cols="100">
ansible 10.71.182.126 -m lineinfile -a &quot;backrefs=yes dest=/usr/local/nginx/conf/nginx.conf regexp=&#039;((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;&#92;&#92;$http_user_agent&#92;&quot;)(.*))&#039; line=&#039;&#92;1&#92;n&#92;2&#92;&quot;http_cdn_src_ip&#92;&quot;:&#92;&quot;&#92;$http_cdn_src_ip&#92;&quot;&#92;4&#039;&quot; 
10.71.182.126 &#124; CHANGED =&gt; &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&quot;backup&quot;: &quot;&quot;, 
&nbsp;&nbsp;&nbsp;&nbsp;&quot;changed&quot;: true, 
&nbsp;&nbsp;&nbsp;&nbsp;&quot;msg&quot;: &quot;line replaced&quot;
&#125;
</textarea><br/><br/>三）有条件的替换，以防止出现替换时因为多次运行相同的Ansible脚本进而多次插入，注意when里面的变量加上单引号为字符串，否则会出现判断不准的问题：<br/><textarea name="code" class="php" rows="15" cols="100">
- name: 配Web系统的Nginx虚拟主机及代码文件初始化配置文件软链接
&nbsp;&nbsp;gather_facts: no
&nbsp;&nbsp;hosts: &quot;&#123;&#123; h &#124; default(&#039;10.71.1*2.126&#039;) &#125;&#125;&quot;
&nbsp;&nbsp;tasks:
&nbsp;&nbsp;- name: 检查http_cdn_src_ip是否存在
&nbsp;&nbsp;&nbsp;&nbsp;shell: cat /usr/local/nginx/conf/nginx.conf&#124;grep http_cdn_src_ip&#124;wc -l
&nbsp;&nbsp;&nbsp;&nbsp;register: hostname_result
&nbsp;&nbsp;&nbsp;&nbsp;ignore_errors: yes 

&nbsp;&nbsp;- debug: msg=&#123;&#123; hostname_result.stdout_lines[0] &#125;&#125;
&nbsp;&nbsp;- name: 将CDN透传过来的客户端访问出口IP写入Nginx日志
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /usr/local/nginx/conf/nginx.conf
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: &#039;&#92;1&#92;n&#92;2&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;&#92;4&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes 
&nbsp;&nbsp;&nbsp;&nbsp;when: hostname_result.stdout_lines[0] == &#039;0&#039;
</textarea><br/><br/>实践2）实践发现假如要写到Yaml文件里，上面单独这一行放Shell里运行可以，但是放到yaml文件里是不行的，怎么办，重新修改调试Ok的文件版本如下所示：<br/><textarea name="code" class="php" rows="15" cols="100">
---
- name: 配置服务器前后台Web系统的Nginx虚拟主机及代码文件初始化配置文件软链接
&nbsp;&nbsp;gather_facts: no
&nbsp;&nbsp;hosts: &quot;&#123;&#123; h &#124; default(&#039;10.244.25.77&#039;) &#125;&#125;&quot;
&nbsp;&nbsp;tags: aixiu_web
&nbsp;&nbsp;tasks:
&nbsp;&nbsp;- name: 将CDN透传过来的客户端访问出口IP写入Nginx日志
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /usr/local/nginx/conf/nginx.conf
&nbsp;&nbsp;&nbsp;&nbsp; #regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;&#92;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#line: &#039;&#92;1&#92;n&#92;2&#92;&quot;http_cdn_src_ip&#92;&quot;:&#92;&quot;&#92;$http_cdn_src_ip&#92;&quot;&#92;4&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: &#039;&#92;1&#92;n&#92;2&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;&#92;4&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes 
&nbsp;&nbsp;&nbsp;&nbsp;tags: addhttpcdnsrcip
</textarea><br/>TASK [将CDN透传过来的客户端访问出口IP写入Nginx日志] **********************************************************************************************************<br/>--- before: /usr/local/nginx/conf/nginx.conf (content)<br/>+++ after: /usr/local/nginx/conf/nginx.conf (content)<br/>@@ -66,6 +66,7 @@<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;xff&quot;:&quot;$http_x_forwarded_for&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;referer&quot;:&quot;$http_referer&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;<br/>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;status&quot;:&quot;$status&quot;&#125;&#039;;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp; access_log&nbsp;&nbsp;/data/logs/nginx/access.log main;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#这一行显示冗余，并没有用，主要看+号。<br/><br/>changed: [10.244.25.77]<br/><br/><br/><br/>实践3）用ansible的insertafter实现：<br/>如果用正则查到某行，在其后面写上也成用insertafter实现，但是这样据前面文章和实践就无法用这个&#92;1&#92;2这样的了，如果打开那个backrefs就需要regexp了，于是这样写实践是Ok的：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;- name: insert after
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /usr/local/nginx/conf/nginx.conf
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: &quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&#92;&quot;http_cdn_src_ip&#92;&quot;:&#92;&quot;$http_cdn_src_ip&#92;&quot;,&#039;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;tags: insertafter
</textarea><br/>--- before: /usr/local/nginx/conf/nginx.conf (content)<br/>+++ after: /usr/local/nginx/conf/nginx.conf (content)<br/>@@ -66,6 +66,7 @@<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;xff&quot;:&quot;$http_x_forwarded_for&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;referer&quot;:&quot;$http_referer&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;<br/>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;status&quot;:&quot;$status&quot;&#125;&#039;;<br/> <br/><br/>实践4）进一步实践发现正则和insertafter混用也是可以的，<br/>也就是说insertafter之后，再加一个regexp正则匹配出&#92;1&#92;2可用在line里，同时加上backrefs: yes，可行的，<br/>如下实践也是能实现的，去掉之前的&#92;1和&#92;n即可，就在它后面插入即可，实践发现并没在后面插入，而是直接替换了，也就是说insertafter失效了，还得按实践2走才Ok，要不就老老实实的按实践3在后在插入，不要引入正则也成，引入正则就失去了insertafter的本来功能了：<br/><textarea name="code" class="php" rows="15" cols="100">
---
- name: insertafter with regexp
&nbsp;&nbsp;gather_facts: no
&nbsp;&nbsp;hosts: &quot;&#123;&#123; h &#124; default(&#039;10.244.25.77&#039;) &#125;&#125;&quot;
&nbsp;&nbsp;tags: aixiu_web
&nbsp;&nbsp;tasks:
&nbsp;&nbsp;- name: insert after
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /usr/local/nginx/conf/nginx.conf
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: &#039;&#92;2&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;&#92;4&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: &#039;((.*)(&#92;&quot;agent&#92;&quot;&#92;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes
&nbsp;&nbsp;&nbsp;&nbsp;tags: insertafter

</textarea><br/><br/>$ansible-playbook iweb_regexp.yml -C -D -t insertafter <br/><br/>PLAY [insertafter with regexp] **************************************************************************************************************<br/><br/>TASK [insert after] *************************************************************************************************************************<br/><br/>--- before: /usr/local/nginx/conf/nginx.conf (content)<br/>+++ after: /usr/local/nginx/conf/nginx.conf (content)<br/>@@ -65,7 +65,7 @@<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;request_uri&quot;:&quot;$request_uri&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;xff&quot;:&quot;$http_x_forwarded_for&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;referer&quot;:&quot;$http_referer&quot;,&#039;<br/>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;<br/>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;status&quot;:&quot;$status&quot;&#125;&#039;;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp; access_log&nbsp;&nbsp;/data/logs/nginx/access.log main;<br/><br/><br/><br/>最后，基础研究,正则替换的基础知识：<br/>对于斜杠来讲,在PHP里即使是单引号，它也是会和类似双引号里的$一样，有被转义：<br/>&#039;/&#92;&#92;&#92;&#92;/&#039;<br/>cat a.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$a = &#039;&#92;&#92;&#92;&#039;&#039;;
echo $a;
?&gt;
</textarea><br/><br/>php a.php <br/>&#92;&#039;<br/><br/>cat reg3.php&nbsp;&nbsp;<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
//$str = &quot;123&#92;$abcdefg&quot;;
$str = file_get_contents(&#039;a3.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
//var_dump(preg_match(&#039;/&#92;&#92;&#92;/&#039;,$str,$rs)); //
var_dump(preg_match(&#039;#&#92;&#92;&#92;#&#039;,$str,$rs)); //
var_dump($rs);
var_dump(preg_match(&#039;#&#92;&#92;&#92;&#92;&#92;#&#039;,$str,$rs));
var_dump($rs);
</textarea><br/><br/>来自微信群：是因为双引号的原因,&#92;#在双引号里，就是&#92;#，&#92;&#92;#在双引号里，就是&#92;#，&#92;&#92;&#92;#在双引号里，就是&#92;&#92;#。<br/><br/>之前http://jackxiang.com/post/6466/研究过反斜杠，没有研究过$，这次主要研究$。<br/>&quot;^-? &#92;&#92;d+$&quot;:这个正则表达式为什么会有两个反斜杠<br/>这要分两步看<br/>首先字符串中的&#92;&#92;被编译器解释为&#92;<br/>然后作为正则表达式，&#92;d又被正则表达式引擎解释为元字符只匹配数字<br/>正则表达式中匹配一个反斜杠要用四个反斜杠，为什么呢？<br/><br/>分析一下“&#92;&#92;&#92;&#92;”，第一个斜杠是转义符，第二个斜杠是斜杠本身，第三个斜杠是转义符，第四个斜杠是斜杠本身。<br/>有2点要清楚：<br/>1.字符串里面表示斜杠就需要两个斜杠如“&#92;&#92;”<br/>2.正则表达式里的斜杠需要转意，是用“&#92;&#92;”标示。<br/>这样就比较好解释：<br/>我们先要表示正则表达式里面的斜杠“&#92;&#92;”，然后再用字符串表示出来。而这2个斜杠分别需要一个转义符，这样就成了4个斜杠在正则表达式里面表示一个斜杠。<br/>From:https://my.oschina.net/airship/blog/411045<br/><br/>php实践如下：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@/data/codesdev/testdemo/php/reg]
#cat reg3.php 
&lt;?php
//$str = &quot;123$ab&quot;;
$str = file_get_contents(&#039;a3.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
var_dump(preg_match(&quot;#&#92;&#92;&#92;&#92;#&quot;,$str,$rs));
var_dump($rs);

[root@/data/codesdev/testdemo/php/reg]
#cat a3.txt 
123&#92;$abcdefg

[root@/data/codesdev/testdemo/php/reg]
#php reg3.php 
int(4)
int(1)
array(1) &#123;
&nbsp;&nbsp;[0]=&gt;
&nbsp;&nbsp;string(1) &quot;&#92;&quot;
&#125;
</textarea><br/><br/><br/>拓展，在Ansible里是Python正则替换时遇到 $符号时会有三个斜杠（http://jackxiang.com/post/8857/），而为什么是三个斜杠呢？<br/><textarea name="code" class="php" rows="15" cols="100">
ansible 10.71.11.39 -m lineinfile -a &quot;backrefs=yes dest=/usr/local/nginx/conf/nginx.conf regexp=&#039;((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;&#92;&#92;$http_user_agent&#92;&quot;)(.*))&#039; line=&#039;&#92;1&#92;n&#92;2&#92;&quot;http_cdn_src_ip&#92;&quot;:&#92;&quot;&#92;$http_cdn_src_ip&#92;&quot;&#92;4&#039;&quot; -C -D 
</textarea><br/>最外层-a&quot;&quot;双引，不看regexp的单引号。<br/>reg.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$str = &quot;123$ab&quot;;
$str = file_get_contents(&#039;a.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
var_dump(preg_match(&quot;#&#92;&#92;&#92;$a#&quot;,$str,$rs));
var_dump($rs);
</textarea><br/><br/>a.txt<br/>123$ab<br/><br/><br/> 运行：php reg.php<br/>会提示：PHP Notice:&nbsp;&nbsp;Undefined variable: ab in /data/codesdev/testdemo/php/reg/reg.php on line 2，<br/>这里的PHP用双引号里面的$会被PHP的解释器解释为一个变量的，如这儿便被认为$ab是一个变量，<br/>用单引号就不存在这个问题，如果非要用单引号，就得转义那个&#92;$,也就让PHP解释器认为这个串里的$仅仅是一个$符号。<br/><br/>三个斜杠的原因，第一个斜杠就是前面讲的双引号里面的$转义为非变量的$,仅仅是一个字符的$，外层双引号为最大，只要外层是引号，在里面加单引也没有用：<br/>$ echo $a<br/>123<br/>$ echo &quot;$a&quot;<br/>123<br/>$ echo &quot;&#92;$a&quot;<br/>$a<br/>$ echo &quot;&#039;&#92;$a&#039;&quot;<br/>&#039;$a&#039;<br/>第二个斜杠是放在正则双引号里面的，因为这个$符号在正则里面看它在正则里的意思是行尾，我们需要认为它在正则里面还是一个$符号，不表示行尾，加第二个斜杠转义表示在正则里的$符号（preg_match&quot;里面是正则的眼光看，这里以#号为正则范围），而这第二个斜杠它在双下号里又认为它是转义的一个斜杠，而不是真正的斜杠，要正的斜杠则要再加一个斜杠，于是有了第三个斜杠。<br/><br/><br/><br/>cat reg2.php <br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
//$str = &quot;123$ab&quot;;
$str = file_get_contents(&#039;a2.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
var_dump(preg_match(&quot;#&#92;&#92;&#92;#&quot;,$str,$rs));
var_dump($rs);
</textarea><br/><br/>cat a2.txt <br/>123&#92;$abcdefg<br/><br/>#php reg2.php <br/>int(4)<br/>int(1)<br/>array(1) &#123;<br/>&nbsp;&nbsp;[0]=&gt;<br/>&nbsp;&nbsp;string(1) &quot;&#92;&quot;<br/>&#125;<br/><br/><br/>RegexBuddy/Patterns的使用方法和来自：https://www.jb51.net/article/104895.htm的一个对照，如下：<br/>test.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$content = &#039;1111111&lt;td&gt;2222222&lt;&#92;/td&gt;3$&#039;;
//&#039;&#92;&#92;&#92;&#92;&#92;/&#039; 第1个&#039;&#92;&#039;转义字符串的第2个&#039;&#92;&#039;，字符串为&#039;&#92;&#039;
//第3个&#039;&#92;&#039;转义第4个&#039;&#92;&#039;，相当于字符串&#039;&#92;&#039;
//第5个&#039;&#92;&#039;转义第4个&#039;/&#039;，相当于字符串&#039;/&#039;
//字符合起来为&#039;&#92;&#92;/&#039; 两个&#039;&#92;&#92;&#039; 正则表达式看做&#039;&#92;&#039;
//$pattern = &#039;/&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$$/&#039;; //最后一个$是正则结尾的意思
$pattern = &#039;/&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$/&#039;;&nbsp;&nbsp;&nbsp;&nbsp;//可以不要正则结尾的$,对3$的$直接转义，加上单引号，不用再转一次，也就是前面的第三个斜杠。
$result = preg_match_all($pattern, $content, $match_result);
if($result)
&nbsp;&nbsp;print_r($match_result);
else
&nbsp;&nbsp;echo(&quot;not match&quot;);
</textarea><br/><br/>正则猫RegexBuddy,Match--&gt;<br/>选个Python吧，能匹配的正则表达式：<br/>&lt;td&gt;[0-9]&#123;7,&#125;&lt;&#92;&#92;/td&gt;&#92;d&#92;$$<br/><br/>Test:<br/>1111111&lt;td&gt;2222222&lt;&#92;/td&gt;3$<br/><br/>而上文里多的斜杠，从哪儿得出来呢？<br/>Copy-&gt;CopyRegex as...-&gt;CopyRegex as PHP string<br/>&#039;&lt;td&gt;[0-9]&#123;7,&#125;&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$&#039;<br/>Copy-&gt;CopyRegex as...-&gt;CopyRegex as PHP &#039;//&#039; preg String<br/>&#039;%&lt;td&gt;[0-9]&#123;7,&#125;&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$%&#039; ，替换为最左最右斜杠：<br/>&#039;/&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$/&#039;&nbsp;&nbsp; #正则猫的正则拷贝Copy结果,加上数字匹配的括号<br/>&#039;/&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$/&#039;&nbsp;&nbsp;#PHP能成功运行并找到正确匹配结果的字符串正则，上面四个斜杠多两个斜杠，&#92;d后面多一个斜杠。<br/><br/>也就是说放到PHP里面还得对斜杠再次转义，让正则认为它是真正的斜杠，而不是PHP里的转义符。<br/><br/>为何四个斜杠加两个斜杠？正则里的反斜杠转义为正则里的字符，但引号里的反斜杠还得转为PHP的正常认为的一个反斜杠，所以得加两个斜杠：<br/>摘自：https://blog.csdn.net/xiaoxiaoniaoer1/article/details/7717669<br/>&nbsp;&nbsp; &quot;&#92;&#92;&quot;==&gt; 反斜杠 <br/><br/>在双引号内使用这些字符时，它们具有特殊的含义<br/>转义字符代码&nbsp;&nbsp;转义字符的含义<br/>&#92; &quot;&nbsp;&nbsp;双引号<br/>&#92; &#039;&nbsp;&nbsp;单引号<br/>&#92; &#92;&nbsp;&nbsp;反斜杠<br/>&#92; n&nbsp;&nbsp;换行符<br/>&#92; r&nbsp;&nbsp;回车符<br/>&#92; t&nbsp;&nbsp;制表符<br/>&#92; $&nbsp;&nbsp;美元符号<br/><br/><br/>实践发现放正则里的内容于外面是双引号单引号无关，只是双引号要转义，只要内容一样，匹配也一样的：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
//$str = &quot;123&#92;$ab&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
$str = &#039;123$ab&#039;;
//$str = file_get_contents(&#039;a.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
var_dump(preg_match(&quot;#&#92;&#92;&#92;$a#&quot;,$str,$rs));
var_dump($rs);
</textarea>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>php reg.php <br/>int(3)<br/>int(1)<br/>array(1) &#123;<br/>&nbsp;&nbsp;[0]=&gt;<br/>&nbsp;&nbsp;string(2) &quot;$a&quot;<br/>&#125;<br/><br/>然而，如果preg_match里的正则用单引号，那就匹配不出来了，如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
//$str = &quot;123&#92;$ab&quot;;
$str = &#039;123$ab&#039;;
//$str = file_get_contents(&#039;a.txt&#039;);
var_dump(strpos($str,&quot;&#92;$a&quot;));
var_dump(preg_match(&#039;#&#92;&#92;&#92;$a#&#039;,$str,$rs));
var_dump($rs);
</textarea><br/><br/>php reg.php <br/>int(3)<br/>int(0)<br/>array(0) &#123;<br/>&#125;<br/><br/><br/>cat reg.php&nbsp;&nbsp;preg_match里的正则用单引号时，少一个斜杠就能匹配出来：<br/>&lt;?php<br/>//$str = &quot;123&#92;$ab&quot;;<br/>$str = &#039;123$ab&#039;;<br/>//$str = file_get_contents(&#039;a.txt&#039;);<br/>var_dump(strpos($str,&quot;&#92;$a&quot;));<br/>var_dump(preg_match(&#039;#&#92;&#92;&#92;$a#&#039;,$str,$rs));<br/>var_dump($rs);<br/><br/>php reg.php&nbsp;&nbsp; <br/>int(3)<br/>int(1)<br/>array(1) &#123;<br/>&nbsp;&nbsp;[0]=&gt;<br/>&nbsp;&nbsp;string(2) &quot;$a&quot;<br/>&#125;<br/><br/><br/>反过来说明啥？说明正则猫RegexBuddy默认是单引进行PHP的匹配，所以前面不用再加两个斜杠转义，证明下：<br/><br/>RegexBuddy采用双引号，也就无法匹配了：<br/>php test.php <br/>not match<br/><br/>得证：<br/>$pattern = &quot;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$%&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//not match <br/>$pattern = &quot;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$$%&quot;;&nbsp;&nbsp;//matched<br/>$pattern = &#039;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$%&#039;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //matched<br/>也就是说RegexBuddy已经指明用单引号了，再回头看前面的：<br/>Copy-&gt;CopyRegex as...-&gt;CopyRegex as PHP &#039;//&#039; preg String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;----它本来就是一个单引号&#039;//&#039; ,指明了，自己瞎搞要用双引号，双引号就再加一个对斜杠的转义即可！！！<br/><br/>回到背景里的这个Ansible，Ansible是用Python写的本质上是一样的道理：<br/>&nbsp;&nbsp;-C, --check&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; don&#039;t make any changes; instead, try to predict some<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of the changes that may occur<br/>&nbsp;&nbsp;-D, --diff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when changing (small) files and templates, show the<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;differences in those files; works great with --check<br/><textarea name="code" class="php" rows="15" cols="100">
ansible 10.71.11.39 -m lineinfile -a &quot;backrefs=yes dest=/usr/local/nginx/conf/nginx.conf regexp=&#039;((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;&#92;&#92;$http_user_agent&#92;&quot;)(.*))&#039; line=&#039;&#92;1&#92;n&#92;2&#92;&quot;http_cdn_src_ip&#92;&quot;:&#92;&quot;&#92;$http_cdn_src_ip&#92;&quot;&#92;4&#039;&quot; -C -D 
</textarea><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;referer&quot;:&quot;$http_referer&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&quot;http_cdn_src_ip&quot;:&quot;$http_cdn_src_ip&quot;,&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&quot;status&quot;:&quot;$status&quot;&#125;&#039;;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp; access_log&nbsp;&nbsp;/data/logs/nginx/access.log main;<br/><br/>10.71.11.39 &#124; SUCCESS =&gt; &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;backup&quot;: &quot;&quot;, <br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;changed&quot;: true, <br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;msg&quot;: &quot;line replaced&quot;<br/>&#125;<br/><br/>而前面用的是双引号，那么改成单引号怎么修改呢？和上面这个还不一样，它是写入到Client端后再运行的，有点不一样，用正则猫试试：<br/>Match:<br/>((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))<br/>Test:<br/>&#039;&quot;agent&quot;:&quot;$http_user_agent&quot;,&#039;<br/>拷贝出来：<br/>&#039;/((.*)(&#92;&quot;agent&#92;&quot;:&#92;&quot;&#92;$http_user_agent&#92;&quot;)(.*))/&#039;<br/><br/><br/><br/><br/>之前那个：<br/>Match:<br/>&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;/td&gt;&#92;d&#92;$$<br/>Test:<br/>1111111&lt;td&gt;2222222&lt;&#92;/td&gt;3$<br/>拷贝出来：<br/>&#039;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$$%&#039;<br/><br/>发现没，那个斜杠是多到四个，但是后面的$并没有加斜杠，说明这只猫并不聪明，还得靠人人为把：<br/>&#92;$整成<br/>&#92;&#92;$<br/><br/>而PHP里对这个 $前的斜杠并不敏感：<br/>$pattern = &#039;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$$%&#039;; <br/>$pattern = &#039;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$$%&#039;; <br/>都能匹配出来：<br/>#php test.php <br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; Array<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; &lt;td&gt;2222222&lt;&#92;/td&gt;3$<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; Array<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; 2222222<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br/><br/>)<br/><br/><br/>总之，不能全信正则猫，实践得出单引号会少用斜杠，双引号就要多用斜杠。：<br/><br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$content = &#039;1111111&lt;td&gt;2222222&lt;&#92;/td&gt;3$&#039;;
$pattern = &quot;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;&#92;$%&quot;;&nbsp;&nbsp;//matched&nbsp;&nbsp;&nbsp;&nbsp; 
//$pattern = &#039;%&lt;td&gt;([0-9]&#123;7,&#125;)&lt;&#92;&#92;&#92;&#92;/td&gt;&#92;d&#92;$%&#039;;&nbsp;&nbsp; //matched
$result = preg_match_all($pattern, $content, $match_result);
if($result)
&nbsp;&nbsp;print_r($match_result);
else
&nbsp;&nbsp;echo(&quot;not match&quot;);
</textarea><br/><br/><br/>EOF
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]Ansible里的后向引用124进行替换,以及正则匹配shell下写在一行和写YAML文件的正则区别，结合正则猫RegexBuddy/Patterns的正确用法。PHP正则匹配反斜杠和美元$的方法以及Ansible单行shell交互和写入yml文件的正则不同写法的原因和对比成功实践及理解。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>