Freek8
Freek8

Reputation: 704

Regex for excluding characters

I'm trying to strip a string of all special characters except a few, plus remove everything between brackets (square, or any other, Including the brackets!). My current regex is:

^[a-zA-Z0-9äöüÄÖÜ;@.]*$
\\[.+\\]
\\<.+\\>
\\s+

All sequences that match one of the above are removed It works fine on e.g.:

Foo Bar[[email protected]]

reducing it too FooBar but not on e.g.:

Foo
[email protected]

removing them completely

Upvotes: 1

Views: 263

Answers (2)

Narendra Yadala
Narendra Yadala

Reputation: 9664

Update: Updating regex as per OP's edit.

You can use the following regex and replace the match with empty string.

\[.*?\]|<.*?>|\s|[^a-zA-Z0-9äöüÄÖÜ;@.]

Upvotes: 1

Ludovic Kuty
Ludovic Kuty

Reputation: 4954

To remove anything between brackets except brackets, you could use the following regex and replace it with an empty string:

/\[[^\]]*\]/

To remove special characters, you could use the one below. It selects everything except what is inside the brackets. So you could once again replace it with the empty string.

/[^a-zA-Z0-9äöüÄÖÜ;@]/

You could use them in sequence or build a bigger one.

In Ruby, I have the following test:

irb(main):001:0> s = "Foo Bar[[email protected]]"
=> "Foo Bar[[email protected]]"
irb(main):005:0* s.gsub(/\[[^\]]*\]|[^a-zA-Z0-9äöüÄÖÜ;@]/, "")
=> "FooBar"

Note that the space has disappeared.

Upvotes: 1

Related Questions