Regular expression for matching spaced letters needed

13/11/2008 - 11:22 von Jan Fischer | Report spam
Hi all,

please tell me, if this isn't the right place to ask.
I am looking for a regular expression which matches "spaced words" in a
sentence like, for example, "M I L L E R" or "B u s h" and replaces them
with "Miller" or "Bush".
I found out that
sub!(/\s?([A-Z])\s([A-Z])\s([A-Z])\s([A-Z])\s([A-Z])\s?/,' \1\2\3\4\5 ')
works for 4 Letters. (Analogue for any other fix number.) But I just
can't figure out how to match any number and how to don't match any
letters of surrounding words.

"A B C D E" -> "ABCDE"
"a b c d" -> "abcd"
"hello A B C test" -> "hello ABC test"

Think you got it. Which regexp will do the job?

Regards Jan
 

Lesen sie die antworten

#1 Jan Fischer
13/11/2008 - 11:25 | Warnen spam
Sorry, wanted it here.

Jan Fischer schrieb:
Hi all,

please tell me, if this isn't the right place to ask.
I am looking for a regular expression which matches "spaced words" in a
sentence like, for example, "M I L L E R" or "B u s h" and replaces them
with "Miller" or "Bush".
I found out that
sub!(/\s?([A-Z])\s([A-Z])\s([A-Z])\s([A-Z])\s([A-Z])\s?/,' \1\2\3\4\5 ')
works for 4 Letters. (Analogue for any other fix number.) But I just
can't figure out how to match any number and how to don't match any
letters of surrounding words.

"A B C D E" -> "ABCDE"
"a b c d" -> "abcd"
"hello A B C test" -> "hello ABC test"

Think you got it. Which regexp will do the job?

Regards Jan

Ähnliche fragen