Min
Min

Reputation: 337

Regex for matching square brackets and number

I have a sentence somewhat like this

Example.[1]

I am trying to write a sentence to leave me with just Example. I currently have

cleanr = re.compile('[\[0-9]\]')
cleantext = re.sub(cleanr, '', raw_html)

and get

Example.

However, if I have any numbers 0-9 anywhere else, they dissapear. How do I remove only things of the form [0-9] without moving lose numereicals outside of brackets, or removing brackets that don't contain a single digit within? ie, I only want to remove things such as [0], [1], [2], [3], ... [9], but not [0Something4].

Upvotes: 1

Views: 1001

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133538

Based on your shown samples, could you please try following.

import re
value='Example.[1]'
re.sub(r"\.\[\d+\]",'',value)

Explanation: Importing re library of python here. Then creating a sample variable named value which has Example.[1] in it. Now as per OP substituting .(DOT) [ followed by 1 or more occurrences of digits ] with NULL in value.

Upvotes: 2

Related Questions