Aishwarya Shiva
Aishwarya Shiva

Reputation: 3406

Regex to remove `.` from a sub-string enclosed in square brackets

I have this regex in C#:

\[.+?\]

This regex extracts the sub-strings enclosed between square brackets. But before doing that I want to remove . inside these sub-strings. For example, the string

hello,[how are yo.u?]There are [300.2] billion stars in [Milkyw.?ay].

should become

hello,[how are you?]There are [3002] billion stars in [Milkyw?ay].

I am not good at forming regular expression so don't have any idea of how to modify my regex.

Live example here: https://regex101.com/r/pL5uA1/1

Upvotes: 0

Views: 218

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174826

To remove all the dots present inside the square brackets.

Regex.Replace(str, @"\.(?=[^\[\]]*\])", "");

DEMO

To remove dot or ?.

Regex.Replace(str, @"[.?](?=[^\[\]]*\])", "");

Upvotes: 3

Related Questions