基于内容的Python正则表达式替换

时间:2017-12-24 08:06:03

标签: python regex

<ul class="products">
<?php
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 12,
        'orderby'        => 'rand',
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                ),
            ),
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
?>

我想使用正则表达式替换“some contents”格式的所有文本,例如s1。但如果单词“Item”在其中,我不想替换它,例如s2。我尝试了下面的代码,但它无法识别Item。

s1='<table>abdjjc eoafl japoge</table>'
s2='<table>abdjjc Item ljapoge</table>'

如果无法完成上述任务,是否可以使用基于长度的正则表达式?我希望仅在整个匹配长度超过一定长度时才以“某些内容”的格式替换文本。我问,因为带有“项目”的文字通常很短,可能是解决它的方法。

3 个答案:

答案 0 :(得分:0)

您可以使用

s1 = re.sub(r'<table.*?>(?:(?!Item).)*?</table>',' ',s1)

可能使用多行DOTALL模式 或者完全使用解析器。 [^Item]是一个否定的字符类,与自己的字符匹配,不是I,不是t,不是e而不是m

答案 1 :(得分:0)

使用零宽度负前瞻可以轻松实现这一点,以确保下一步不会出现Item

In [19]: s1='<table>abdjjc eoafl japoge</table>'

In [20]: s2='<table>abdjjc Item ljapoge</table>'

In [21]: re.sub(r'^<table>(?!.*Item).*</table>', 'FOO', s1)
Out[21]: 'FOO'

In [22]: re.sub(r'^<table>(?!.*Item).*</table>', 'FOO', s2)
Out[22]: '<table>abdjjc Item ljapoge</table>'

此处提到的负前瞻模式为(?!.*Item),位于<table>之后,以确保以后随时在字符串中不会出现Item

答案 2 :(得分:0)

您可以尝试使用某个功能,只提取您想要的内容并保留原样:

import re
s1='<table>abdjjc eoafl japoge</table>'
s2='<table>abdjjc Item ljapoge</table>'

pattern=r'(?:<table>)(\w.+?)(?:<\/table>)'

s_all=[s1,s2]
def replacing(s):
    store=[]
    for i in s_all:
        if 'Item' in i:  #if the condition is true don't do anything to text
            store.append(i)

        else:            #if condition is false now replace that stuff with your 'some content'
            match=re.search(pattern,s1)

            replace=re.sub(match.group(1),' some contents ',s1)
            store.append(replace)

    return store

print(replacing(s_all))

输出:

['<table> some contents </table>', '<table>abdjjc Item ljapoge</table>']
#now save this result to new file or whatever you want to do with it.