3

I would like to title case every sentence ending with parentheses containing uppercase letters only NB: do not title case certain words such as "for, and, the" for instance.

So the original sentence is:

foundation for economic education (FEE)

And it should be like that:

Foundation for Economic Education (FEE)

Any help is much appreciated!

Here is my "faulty" code, it capitalizes everything before the parenthesis...

(^[\p{L}\W]+)([(][\p{Lu}]{3,}[)]$)

\U\1\2
Toto
  • 89,455
  • 62
  • 89
  • 125
Althussa
  • 31
  • 3
  • 1
    https://stackoverflow.com/a/41061123/4251338 – ssr1012 Dec 30 '20 at 06:10
  • Thanks for your help, it works fine for title casing the WHOLE sentence (except certain words ie "and, the"...) BUT how to apply it only for a sentence ending with parentheses? – Althussa Dec 30 '20 at 06:35
  • 1
    You should clear out what is a sentence in your case : Does it ends with a period ? and new line ? Please provide more positive example and negative example. – Gabriel Glenn Dec 30 '20 at 07:17
  • What do you want your example sentence to look like afterwards? – Shawn Dec 30 '20 at 08:20
  • The original sentence ends with a parenthese (so the code would be [)]$) and inside there are only uppercase letters. => So for instance, the original sentence is: foundation for economic education (FEE) => And I would like it to be like that: Foundation for Economic Education (FEE) Thanks for your help! – Althussa Dec 30 '20 at 09:00
  • Oh, so you're wanting to know how to only apply the casing to sentences matching a certain pattern, not do something special with the text in parenthesis? – Shawn Dec 30 '20 at 11:16
  • That is not code, that is a regex and some escape sequences. While it might be tempting to cram all of the functionality into a single regex, it certainly is an unnecessarily difficult way to solve your problem. – TLP Dec 30 '20 at 15:15
  • This question has an answer here: https://stackoverflow.com/q/77226/725418 – TLP Dec 30 '20 at 15:22
  • In short, read the [faq](https://perldoc.perl.org/perlfaq4#How-do-I-capitalize-all-the-words-on-one-line). Apply `Text::Autoformat` to the string when you can match parentheses in the last word. – TLP Dec 30 '20 at 15:33
  • 1
    If `for` in `foundation for economic education` should stay uncapitalized (as is shown in the question and comments), this is not a duplicate of [How can I capitalize the first letter of each word in a string in Perl?](https://stackoverflow.com/questions/77226/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-in-perl). – Ryszard Czech Dec 30 '20 at 22:02
  • @RyszardCzech Yes, it is, you just didn't read the faq, like the answer said to do. – TLP Dec 30 '20 at 22:43
  • 1
    Is it in fact an R question? Why did you tag it with Perl? – Wiktor Stribiżew Dec 31 '20 at 10:30
  • Althussa, please clarify what programming environment this question is related to and if `autoformat($x, { case => 'highlight' })` indeed solves the problem. – Wiktor Stribiżew Jul 03 '21 at 10:51

1 Answers1

1

The regex that matches a string with no uppercase letters ending with (...) substring is, for example:

/^(\P{Lu}*)(?=\([^()]*\)$)/

See the regex demo. There are other ways, but this way makes it more convenient to build the replacement part, RHS, in Perl later.

Pattern details:

  • ^ - start of string
  • (\P{Lu}*) - Group 1: any zero or more chars other than uppercase letters
  • (?=\([^()]*\)$) - a positive lookahead that requires (, then any zero or more chars other than ( and ) and then a ) at the end of the string ($).

Then you may use the Lingua::EN::Titlecase like this:

#!/usr/bin/perl
use feature 'say';
use strict;
use warnings;
use Lingua::EN::Titlecase;

my $tc = Lingua::EN::Titlecase->new();
my $str = "foundation for economic education (FEE)";
say $str =~ s{^(\P{Lu}*)(?=\([^()]*\)$)}{ $tc->title($1) }re;

Output: Foundation for Economic Education (FEE).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • thanks for your help! Unfortunetaly for groupings, my regex flavor can only use the followings statements "\1", "\2", etc..., So in order to uppercase a letter, I add this "\1>". I can't use the "Lingua::EN::Titlecase" procedure... – Althussa Dec 31 '20 at 09:16
  • @Althussa What programming language are you using? If it is not Perl, what is it? R? – Wiktor Stribiżew Dec 31 '20 at 09:19
  • 1
    @Althussa Please find some time to clarify what you are doing, in order to put the question to rights. – Wiktor Stribiżew Dec 31 '20 at 11:19
  • @Althussa Please let me know if your question is correctly answered or not, since if it is an unclear question (and it is now), I'd rather cast a delete vote rather than reopen. – Wiktor Stribiżew Jan 09 '21 at 21:57