Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Replace tags but preserve inner text V2
    primarykey
    data
    text
    <p>I've got a script to do search and replace. it's based on a script <a href="https://stackoverflow.com/questions/4236243/python-replace-tags-but-preserve-inner-text">here</a>. It was modified to accept file as input but it does not seem to recognize regex well.</p> <p>The script:</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, os import re import glob _replacements = { '[B]': '**', '[/B]': '**', '[I]': '//', '[/I]': '//', } def _do_replace(match): return _replacements.get(match.group(0)) def replace_tags(text, _re=re.compile('|'.join((r) for r in _replacements))): return _re.sub(_do_replace, text) def getfilecont(FN): if not glob.glob(FN): return -1 # No such file text = open(FN, 'rt').read() text = replace_tags(text, re.compile('|'.join(re.escape(r) for r in _replacements))) return replace_tags(text) scriptName = os.path.basename(sys.argv[0]) if sys.argv[1:]: srcfile = glob.glob(sys.argv[1])[0] else: print """%s: Error you must specify file, to convert forum tages to wiki tags! Type %s FILENAME """ % (scriptName, scriptName) exit(1) dstfile = os.path.join('.' , os.path.basename(srcfile)+'_wiki.txt') converted = getfilecont(srcfile) try: open(dstfile, 'wt+').write(converted) print 'Done.' except: print 'Error saving file %s' % dstfile print converted #print replace_tags("This is an [[example]] sentence. It is [[{{awesome}}]].") </code></pre> <p>What I want is to replace </p> <pre><code>'[B]': '**', '[/B]': '**', </code></pre> <p>with only one line like this as in regex</p> <pre><code>\[B\](.*?)\[\/B\] : **\1** </code></pre> <p>That very would be helpful with BBcode tags like this:</p> <pre><code>[FONT=Arial]Hello, how are you?[/FONT] </code></pre> <p>Then I can use something like this</p> <pre><code>\[FONT=(.*?)\](.*?)\[\/FONT\] : ''\2'' </code></pre> <p>But I can not seem to be able to do that with this script. There are another ways to do regex search and replace in the original source of this script but it works for one tag at a time using re.sub. Other advantage of this script that I can add as many line as I want so I can update it later.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload