Search code examples
pythonregexspecial-characters

python regular expression to match dots and brackets


I am trying to write a regex to delete the below patterns from text file:

[...] (one or more dots in between [] )
[. . . .] (one or more dots between [] with space in them)

I tried things like this [^A-Za-z]\[.*\] however it matched all the words in between them. eg for this is [...] what you want to keep. [....] [. . . ] [ . ..]and [this] It matched from the first [ to the last ]. Could someone please help? Many thanks.


Solution

  • You can use the below regex.

    \[(\s*\.\s*)*\]
    

    You need to escape . as it represents any character in regex terms, so the correct usage is \..