1

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].

Min
  • 327
  • 1
  • 4
  • 22
  • 1
    Use `\[\d]` or `\[\d+]` – Wiktor Stribiżew Mar 14 '21 at 01:40
  • I think one of those backslashes is misplaced -- should be `\[[0-9]\]` or just `\[\d\]` – Samwise Mar 14 '21 at 02:50
  • 1
    Also, this is how to [**match a dot**](https://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot). – Wiktor Stribiżew Mar 14 '21 at 20:39
  • 3
    [Regex : matching integers inside of brackets](https://stackoverflow.com/questions/59793742/regex-matching-integers-inside-of-brackets) is only a partial duplicate but not-so-well-defined problem with no source code. If needed that question can be closed a dupe of current one. – anubhava Mar 15 '21 at 19:28
  • 1
    Right, partial of the first, partial of the second, and it is a [case described by Code Gray](https://meta.stackoverflow.com/questions/405053/should-a-question-without-source-code-in-it-be-closed#comment823358_405057) where you do not need to connect any more than 2 "dots". – Wiktor Stribiżew Mar 19 '21 at 09:52
  • 4
    We often assume that connecting 2 or more dots might be quite easy. When it is not. Not for me at least when starting fresh with a technology. – anky Mar 21 '21 at 17:37
  • 2
    There is adding `\.` + `\[\d+]`, **what is not evident here** ? – Wiktor Stribiżew Mar 21 '21 at 18:57
  • 4
    I understand the points made here. My view on this is if there is an exact dupe which exists, then we can close the question. However if there isn't any , we should let OP have their deserved answer. Whilst I am for - maintaining the sanity of any tag, however not at the cost of user experience. Ultimately we are a Q&A site. I may be wrong but that's my layman view. – anky Mar 22 '21 at 05:12
  • Then there was no point of implementing multiple duplicate feature. – Wiktor Stribiżew Mar 22 '21 at 10:34
  • 4
    Attaching 2 different links to a question and this answer is providing answer with both the functionalities, now what makes site better user experience? 1- An answer(this one) which has complete answer(OP's question), 2- Or adding 2 different links where OP has to search for the answers. Anyone will go for 1st option in terms of Good user experience. BTW these were NOT 2 same dupes they are link with 2 different solutions. – RavinderSingh13 Mar 22 '21 at 10:37

1 Answers1

2

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.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93