Sasa
Sasa

Reputation: 11

Regular expression - removing brackets and what is inside if they follow a certain structure

I have a text example like

Hello my car is [tue mar 31 2020 - antoni mark ] [nissan]

I want to remove the square brackets and what is inside it if it has the structure "tue mar 31 2020 - antoni mark".

So the text_filtered should look like "Hello my car is [nissan]" I am trying to use this pattern "\[.*?\]", but it removes all square brackets.

Any idea on how to modify the pattern?

Upvotes: 1

Views: 47

Answers (1)

Bohemian
Bohemian

Reputation: 425358

This matches a space then square brackets containing a date in your format then a dash then non-square brackets:

" \[\w{3} \w{3} \d\d? \d{4} - [^]]+]"

See live demo.

Matching the leading space will prevent the result after replacing with blank from containing double spaces.

Upvotes: 1

Related Questions