擅长:python、mysql、java
<pre><code>import re
Variable = "The sun shines, that's fine [not, for, everyone] and if it rains, it Will Be better."
Variable1 = re.sub("\[[^]]*\]", lambda x:x.group(0).replace(',',''), Variable)
</code></pre>
<p>首先需要找到需要重写的字符串部分(使用<code>re.sub</code>执行此操作)。然后你重写那些部分。</p>
<p>函数<code>var1 = re.sub("re", fun, var)</code>的意思是:在te变量<code>var</code>中找到符合<code>"re"</code>的所有子串;用函数<code>fun</code>处理它们;返回结果;结果将保存到<code>var1</code>变量。</p>
<p>正则表达式“[[^]]*”表示:查找以<code>[</code>(re中的<code>\[</code>)开头、包含除<code>]</code>(re中的<code>[^]]*</code>)之外的所有内容并以<code>]</code>(re中的<code>\]</code>)结尾的子字符串。</p>
<p>对于找到的每个事件,运行一个函数将此事件转换为新事件。
功能是:</p>
<pre><code>lambda x: group(0).replace(',', '')
</code></pre>
<p>这意味着:获取找到的字符串(<code>group(0)</code>),用<code>''</code>替换<code>','</code>(换句话说,删除<code>,</code>),并返回结果。</p>