How to break everything in []

I am trying to break the leading text inside [], including []as shown below

title  = "[test][R] D123/Peace123456: panic:"
print title
title = title.strip('[.*]')
print title

CONCLUSION: -

test][R] D123/Peace123456: panic:

EXPECTED OUTPUT:

[R] D123/Peace123456: panic:
+4
source share
1 answer

You need a non-greedy Regex to match the first []from the beginning, and re.subto replace:

In [10]: title  = "[test][R] D123/Peace123456: panic:"

# `^\[[^]]*\]` matches `[` followed by any character
# except `]` zero or more times, followed by `]`
In [11]: re.sub(r'^\[[^]]*\]', '', title)
Out[11]: '[R] D123/Peace123456: panic:'

# `^\[.*?\]` matches `[`, followed by any number of
# characters non-greedily by `.*?`, followed by `]`
In [12]: re.sub(r'^\[.*?\]', '', title)
Out[12]: '[R] D123/Peace123456: panic:'
+6
source

Source: https://habr.com/ru/post/1694939/


All Articles