How to Replace Text Between Two Custom Balises Using Python ?

Introduction

Custom "balises" (tags) like

1
[MemberOnly]...[/MemberOnly]  or <MemberOnly>...</MemberOnly>

are commonly used to mark parts of text for special handling, such as restricting access to members or controlling visibility in content management systems. In this article, you'll learn how to identify, replace, and manipulate text between custom balises using Python and regular expressions.

We want to:

  1. Replace the content between two custom tags with a placeholder message.
  2. Optionally remove only the tags but keep the content.
  3. Detect the presence of these tags in the text.
  4. Choose the best format for custom balises.
  5. Style the replaced section for rendering in HTML.

Replacing Content Between Two Balises

If you have content like:

1
2
3
content = """dummy text dummy text 
[MemberOnly]this part is restricted[/MemberOnly]
more text here"""

You can use Python’s re.sub to replace the entire section:

1
2
3
4
5
6
7
8
import re

content_cleaned = re.sub(
    r"\[MemberOnly\].*?\[/MemberOnly\]",
    "<span class='member_only_section'>This content is reserved for members only. Please sign in to access it.</span>",
    content,
    flags=re.DOTALL
)

Use re.DOTALL so the regular expression matches across multiple lines.

Removing Only the Tags, Keeping the Content

If you want to keep the content but just remove the tags:

1
content_tagless = re.sub(r'\[/?MemberOnly\]', '', content)

This matches both [MemberOnly] and [/MemberOnly] and removes them, preserving the text in between.

Detecting the Presence of Balises

You can check whether such tags exist using re.search:

1
2
if re.search(r"\[MemberOnly\].*?\[/MemberOnly\]", content, re.DOTALL):
    print("Found a member-only section")

Or more generally:

1
2
if "[MemberOnly]" in content:
    print("Member-only tag detected")

Choosing the Right Balise Format

You can use different formats depending on your needs:

1
2
3
4
5
6
| Format                   | Example                                                   | Pros                         |
| ------------------------ | --------------------------------------------------------- | ---------------------------- |
| HTML-style tags          | `<member-only>content</member-only>`                      | Works well in HTML/JS tools  |
| Square brackets (BBCode) | `[MemberOnly]content[/MemberOnly]`                      | Common in plain text systems |
| HTML comments            | `<!-- member_only_start --> ... <!-- member_only_end -->` | Invisible in HTML            |
| Shortcodes (like Hugo)   | `{{< MemberOnly >}} ... {{< /MemberOnly >}}`            | Familiar for static websites |

If you plan to render content as HTML, use angle brackets ([HTML REMOVED]). If you're working with plain text or markdown, square brackets () or comment-style are safer.

Optional: Styling the Output in HTML

To visually highlight the member-only section in rendered HTML, you can use CSS:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<style>
.member_only_section {
  background-color: #fff4cc;
  border: 1px dashed #ffaa00;
  padding: 1em;
  border-radius: 8px;
  font-style: italic;
  color: #555;
}
</style>

Use this class in your HTML output like:

1
<span class="member_only_section">This content is reserved for members only. Please sign in to access it.</span>

Final Thoughts

Using Python’s regular expressions, it’s easy to build flexible content parsers and editors that respect your custom tags. Whether you're building a publishing tool, CMS, or preprocessing markdown, this approach keeps your pipeline simple and powerful.

Need to support more tag types or nested structures? Consider switching to an actual parser like BeautifulSoup (for HTML) or building a markdown processor with custom extensions.

References

Links Site
re — Regular expression operation docs.python.org