简体   繁体   中英

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] this is [...] what you want to keep. [....] [. . . ] [ . ..]and [this] this is [...] what you want to keep. [....] [. . . ] [ . ..]and [this] It matched from the first [ to the last ]. Could someone please help? Many thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM