8

I would like to use regular expressions (REGEX) to capitalise the first letter of each word in a sentence.

I have achieved the same result in programming languages, but it seems that using regular expressions would be more concise.

David
  • 2,322
  • How would you handle the case where you have an abbreviation? Please provide a samples from the other languages you have done this in. – David May 03 '14 at 02:06
  • like "test testing" should return "Test Testing" im working on it with MSL and this code do exactly the same thing, but i know there is a way to use regex

    alias reg { var %x = 1, %y = $$1, %z while (%x <= $numtok(%y,32)) { %z = %z $+($upper($left($gettok(%y,%x,32),1)),$mid($gettok(%y,%x,32),$iif($len($gettok(%y,%x,32)) == 1,$remove(%x,$right($gettok(%y,%x,32),1)),$+(-,$calc($len($gettok(%y,%x,32)) - 1))))) inc %x } return %z }

    – Sirius_Black May 03 '14 at 02:07
  • Also, which type of regex are you wanting to use? e.g. PCRE? POSIX? There are subtle differences, and it makes a difference as to where and/or what languages you want to use the REGEX in. – David May 03 '14 at 02:13
  • Is that mirc scripting language? – David May 03 '14 at 02:16

1 Answers1

7

Example using sed command.

~$ echo "foo bar" | sed  's/^\(.\)/\U\1/'

Where:

  • the ^ represents the start of a line.
  • . matches any character.
  • \U converts to uppercase.
  • \( ... \) specifies a section to be referenced later (as \1 in this case).
stderr
  • 10,394
  • 2
  • 32
  • 49