I apologize if this question has already been answered, but I searched and can not find the answer. I am trying to write a regular expression that will match all leading and trailing spaces, the spaces between the opening and closing brackets and the word, but will not match the spaces between the words. The following are examples of the formatted data format that I process:
[Header] [ SomeSpace] [ Some1 More Space 15 ]
no leading and trailing space, no spaces between brackets and just one word.
some leading and final space, the space between the opening bracket and the finite space.
some leading space, a space between the word and numbers, the space between the opening and closing brackets, and the ending space.
The closest one regex I came up with is:
/[^\[\]a-zA-Z\d]/
But I can’t cancel only the spaces between words and numbers ...
The ruby code I use as a workaround is:
line.gsub!(/^\s*/, "") line.gsub!(/\[/, "") line.gsub!(/\]/, "") s = line.gsub!(/^\s*|\s*$/, "") s = "[" + s + "]\n"
Obviously not very pretty ...
Any help on optimizing this into the elegant gsub line is welcome.
Thanks!
Lee
source share