Regex 降价不适用于其他空间?

Regex 降价不适用于其他空间?,regex,python-3.x,markdown,Regex,Python 3.x,Markdown,在尝试将md转换为html文件时,我使用此正则表达式将所有匹配代码的结尾替换为\n到\n\t。问题在于使用search()时匹配,而不是使用sub()时匹配。我需要帮助 import markdown2 import re code_text = """* But why did the `is` operator evaluated to `False`? Let's see with this snippet. ```py class WTF(object): def __

在尝试将
md
转换为
html
文件时,我使用此正则表达式将所有匹配代码的结尾替换为
\n
\n\t
。问题在于使用
search()
时匹配,而不是使用
sub()
时匹配。我需要帮助

import markdown2
import re

code_text = """* But why did the `is` operator evaluated to `False`? Let's see with this snippet.
  ```py
  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
  ```

  **Output:**"""

print("PROBLEM :: \n", markdown2.markdown(code_text))

pattern = re.compile(r"```py(?P<code>.*?)```", re.DOTALL)

code_only = pattern.search(code_text).group("code")

code_only = code_only.replace("\n", "\n\t")

print("SOLUTION 1:: \n", markdown2.markdown(code_only))

###################################################
# Replacing all code occurrences in the given string

code_only = pattern.sub(lambda m: (m.group("code")).replace("\n", "\n\t"), code_text)

print("SOLUTION 2:: \n", markdown2.markdown(code_only))
输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>

<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>

<p><strong>Output:</strong></p></li>
</ul>
解决方案2:
  • 但是为什么将
    操作符计算为
    False
    ?让我们看看这个片段

    类WTF(对象): definit(自身):打印(“I”) defdel(自身):打印(“D”)

    输出:

  • PROBLEM :: 
     <ul>
    <li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
    <code>py
    class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
    </code></p>
    
    <p><strong>Output:</strong></p></li>
    </ul>
    
    SOLUTION 1:: 
     <pre><code>  class WTF(object):
        def __init__(self): print("I ")
        def __del__(self): print("D ")
    </code></pre>
    
    SOLUTION 2:: 
     <ul>
    <li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>
    
    <p>class WTF(object):
        def <strong>init</strong>(self): print("I ")
        def <strong>del</strong>(self): print("D ")</p>
    
    <p><strong>Output:</strong></p></li>
    </ul>
    
尝试将
code\u文本
更改为

PROBLEM :: 

 <p>```py</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>a = "some<em>string"
      id(a)
      140420665652016
      id("some" + "</em>" + "string") # Notice that both the ids are same.
      140420665652016
      ```</p>

      <h3>This is a heading</h3>
    </blockquote>
  </blockquote>
</blockquote>

<p><code>py
print("Hello World")
</code></p>

SOLUTION 1:: 
 <pre><code>&gt;&gt;&gt; a = "some_string"
&gt;&gt;&gt; id(a)
140420665652016
&gt;&gt;&gt; id("some" + "_" + "string") # Notice that both the ids are same.
140420665652016
</code></pre>

SOLUTION 2:: 
 <pre><code>&gt;&gt;&gt; a = "some_string"
&gt;&gt;&gt; id(a)
140420665652016
&gt;&gt;&gt; id("some" + "_" + "string") # Notice that both the ids are same.
140420665652016
</code></pre>

<h3>This is a heading</h3>

<pre><code>print("Hello World")
</code></pre>
它工作得很好

输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li>
<p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>
<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>
</li>
</ul>
<p><strong>Output:</strong></p>
解决方案2: 这是一个标题 编辑1

尝试使用Pypi标记

输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>

<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>

<p><strong>Output:</strong></p></li>
</ul>
解决方案2:
  • 但是为什么将
    操作符计算为
    False
    ?让我们看看这个片段

    类WTF(对象): definit(自身):打印(“I”) defdel(自身):打印(“D”)

输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>

<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>

<p><strong>Output:</strong></p></li>
</ul>
编辑2:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>

<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>

<p><strong>Output:</strong></p></li>
</ul>

sub()
工作正常。我刚刚确认了它,但是由于代码在每行末尾都有
\n\t
,所以在运行它之后,它变成了
\n\t\t
,现在
标记()
不起作用。

您可以使用它(Python标记可以输出带有HTML或XHTML样式标记的文档)。尝试了,没有起作用。
sub()
有效。很抱歉。如果有额外的空间,mardown不工作。