1

I'm looking for a way to change the first character of every word in a sentence form lowercase to uppercase. I already read the following answer but it doesn't work.

I tried to use \U to replace the first letter as an uppercase letter. But it returns \U as the replacement, not the first letter. May someone take a look at http://regexr.com?36h59

Thank you in advance!

Community
  • 1
  • 1
  • It depends on the regex variant, not all variants can do this. So where/how will you execute this for real? – Wolph Sep 29 '13 at 00:53
  • 1
    please specify a language. – Math chiller Sep 29 '13 at 01:00
  • Nothing you can do if the regex implementation you're using doesn't support the functionality to do this (`\U`). – Bernhard Barker Sep 29 '13 at 01:08
  • Nobody knows what you think is a 'word'. Until, or unless, you define that, there is nothing that can help you. No utilities, no guesses, nothing, letters can start after any non-letters. The non-letter class is humonguous. –  Sep 29 '13 at 02:28

4 Answers4

1

If text transformations are possible depends on the regexp implementation. Most standard implementations base off Perl regular expressions and do not support this.

A lot of text editors however do provide some way of transformations as they do not have any other capabilities of processing a regular expression match. For example the answer in your linked question refers to an editor called “TextPad”. These transformations are often non-standard and can also differ a lot depending on what tool you use. When using programming languages however, you don’t really need those features built into the regular expression syntax, as you can easily store the match and do some further processing on your own. A lot language also allow you to supply a function which is then called to process every replacement individually.

If you tell us what language you are using, we might be able to help you further.

Some examples

JavaScript:

> text = 'anleitungen gesundes wohnen';
> text.replace(/(\w+)/g, function(x) { return x[0].toUpperCase() + x.substring(1) });
'Anleitungen Gesundes Wohnen'

Python:

>>> import re
>>> text = 'anleitungen gesundes wohnen'
>>> re.sub('(\w+)', lambda x: x.group(0).capitalize(), text)
'Anleitungen Gesundes Wohnen'
poke
  • 369,085
  • 72
  • 557
  • 602
  • I use the addon "SEO Tools for Excel" by Niels Bosma. The built-in "RegexpReplace" Function is based on. Net regular expressions. http://nielsbosma.se/projects/seotools/functions/regexpreplace/ – Olaf Pleines Sep 29 '13 at 01:09
  • Thank you for your answer, but it is more a kind of end-user problem ;-) – Olaf Pleines Sep 29 '13 at 01:12
  • While the .NET regular expressions do support a similar replacement function as shown in the examples above, I’m afraid you won’t be able to use them in that Excel function. However, there is a built-in Excel function that does exactly what you need: It capitalizes every word. The function is called `PROPER` (`GROSS2` in German). So you could just use `=PROPER("anleitungen gesundes wohnen")` or even `=PROPER(RegexpIsFind(...))` to transform the text. – poke Sep 29 '13 at 01:15
0

javascript

 str.replace(/' '([a-z])+/, foo);

function foo(x){
    return x.toUpperCase();
}
Math chiller
  • 4,123
  • 6
  • 28
  • 44
0
$ echo 'xyzzy plugh dwarf' | sed 's/\(\<.\)\.*/\u\1/g' 
Xyzzy Plugh Dwarf
Mike Makuch
  • 1,788
  • 12
  • 15
-1

If your coding using PHP check out the manual here: http://php.net/manual/en/function.ucwords.php

jonny
  • 31
  • 4