Changing case of the first letter of words in string

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Changing case of the first letter of words in string

    I want to make the case of the first letter of all the words in a
    selected string to upper case. The code

    s/\b(\w+)/\u$1\E/g;

    enables me to do this for the whole document.

    But the string I want to match and operate on is all instances of
    text following the string

    SCTN:

    as in

    SCTN: News Analysis

    or

    SCTN: Special Report

    But when I try

    s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;

    nothing seems to change? : (

    Bis
    --
    Bis Guest

  2. Similar Questions and Discussions

    1. Changing from half letter to letter document size
      I have a document that was created in the size it will be (5.5" x 8.8") and it will be punched on the spine side. It has a slightly larger margin on...
    2. No Pcase? how do I capitolize the first letter of aword, with the rest of the word lower case?
      I know I can go one way or another, Ucase(var) all upppercase, or Lcase(var) all lowercase, but how can I get a Propercase? I've seen this in VB,...
    3. opening a file whose letter case is unknown
      Say we have a file named "ABC" , yet A B and C can be upper or lower case each ( 8 combinations ). how can one neatly open this file without...
    4. String question: Returning portion of string with words surrounding highlighted search term?
      I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string,...
    5. How to pick out words from a non-delimited string?
      Hi, Since Perl excels at string parsing, I thought this would be the right group to ask my question. If not, I apologize for the confusion. I...
  3. #2

    Default Re: Changing case of the first letter of words in string



    Bis wrote:
    > I want to make the case of the first letter of all the words in a
    > selected string to upper case. The code
    > s/\b(\w+)/\u$1\E/g;
    > enables me to do this for the whole document.
    > But the string I want to match and operate on is all instances of text
    > following the string
    > SCTN:
    > as in
    > SCTN: News Analysis
    > or
    > SCTN: Special Report
    > But when I try
    > s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
    > nothing seems to change? : (
    > Bis
    My guess (without actually trying) is the word boundary marker (\b).
    Since you're grabbing all preceding whitespace you can't (in theory)
    expect it to find whitespace before your word. So... try this:

    s/(SCTN:\s*)(\w+)/$1\u$2\E/g;

    Gabriel Cooper Guest

  4. #3

    Default Re: Changing case of the first letter of words in string

    Bis <biswadip.dasgupta@centaur.co.uk> wrote:
    > I want to make the case of the first letter of all the words in a
    > selected string to upper case. The code
    >
    > s/\b(\w+)/\u$1\E/g;
    >
    > enables me to do this for the whole document.
    >
    > But the string I want to match and operate on is all instances of text
    > following the string
    >
    > SCTN:
    >
    > as in
    >
    > SCTN: News Analysis
    >
    > or
    >
    > SCTN: Special Report
    >
    > But when I try
    >
    > s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
    >
    > nothing seems to change? : (
    That will only change the first letter of the first word after 'SCTN:'.
    That's what you're seeing, right? If you want to change *all* the words
    after SCTN: to start with uppercase (and leave the ones before it alone),
    maybe something like this:

    my $text = q(leave this alone SCTN: this isn't capitalized but should be);
    if (/SCTN:/) {
    my ($before, $after) = split /SCTN:/, $text, 2;
    $after =~ s/(\S+)/\u$1/g;
    $text = $before . 'SCTN:' . $after;
    }
    print $text;

    I used \S instead of \w in an attempt to handle contractions such as
    "can't" and "don't", etc.

    Now I'd almost bet someone (John Krahn?) will come up with a more clever
    approach and make it into a one-liner. :-)


    David K. Wall Guest

  5. #4

    Default Re: Changing case of the first letter of words in string

    Bis wrote:
    >
    > I want to make the case of the first letter of all the words in a
    > selected string to upper case. The code
    >
    > s/\b(\w+)/\u$1\E/g;
    >
    > enables me to do this for the whole document.
    >
    > But the string I want to match and operate on is all instances of
    > text following the string
    >
    > SCTN:
    >
    > as in
    >
    > SCTN: News Analysis
    >
    > or
    >
    > SCTN: Special Report
    >
    > But when I try
    >
    > s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
    >
    > nothing seems to change? : (

    s{^SCTN:\s*(.*)}{ ($a = $1) =~ s[(\w+)][\u$1]g; $a }e;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default Re: Changing case of the first letter of words in string

    On Friday, August 22, 2003, at 05:06 PM, bis wrote:
    > Thanks Gabriel - your suggested code
    >
    > s/(SCTN:\s*)(\w+)/$1\u$2\E/g;
    >
    > is an improvement - it does capitalise the first
    > letter - but only of the first word after "SCTN: " so
    > i get something like
    >
    > SCTN: This is a section name
    >
    > What I need is
    >
    > SCTN: This Is A Section Name
    >
    > hope that makes sense! :)
    Well, let's see if we can get a little closer:

    s/(SCTN:\s*)(.+)$/join '', $1, map { ucfirst $_ } split /( )/, $2/ge;

    See if that helps any.

    James

    James Edward Gray II Guest

  7. #6

    Default Re: Changing case of the first letter of words in string

    *Please* CC the mailing list when you answer. I'm writing it so everyone
    on the list could learn something, not just to fix your program or solve
    your particular problem.

    bis wrote:
    > Tnaks zsdc and JEGII.
    >
    > --- zsdc <zsdc@wp.pl> wrote:
    >
    >>It gives a syntax error. Maybe try this:
    >>
    >> #!/usr/bin/perl -p
    >> s/(\s)(.)/$1\u$2/g if/SCTN:/;
    >
    > This capitalises the first letter of every word in the
    > whole document.
    No, it doesn't. Only the lines containing "SCTN:" Have you run it?
    >>or instead of /SCTN:/ use /^SCTN:/
    >
    > This doesn't do anything.
    >
    > or /^\s*SCTN:/
    >
    > Nor does this.
    Well... Yes, it does. How did you run it, anyway?
    > I think the title of my query was misleading - what I
    > maybe should have said was "Uppercasing the first
    > letter and lowercasing all the other letters of every
    > word in a mixed case part of a string"?
    This is much harder. It's easy to make the output like this:

    I'Ve Been Reading O'Reilly Books

    OR:

    I've Been Reading O'reilly Books

    but not:

    I've Been Reading O'Reilly Books

    which is correct. I suggest you to only uppercase characters, but if you
    have to also lowercase the other ones then try:

    #!/usr/bin/perl -p
    s/(\s)(\S+)/$1\L\u$2/g if/^\s*SCTN:/;

    or

    #!/usr/bin/perl -p
    s/\b(\w+)/\L\u$1/g if/^\s*SCTN:/;

    (and maybe add:

    use locale;

    at the beginning) and see which suits your needs better. You might find
    better examples in the Cookbook.

    -zsdc.

    Zsdc Guest

  8. #7

    Default Re: Changing case of the first letter of words in string

    --- zsdc <zsdc@wp.pl> wrote: > *Please* CC the mailing
    list when you answer. I'm
    > writing it so everyone
    > on the list could learn something, not just to fix
    > your program or solve
    > your particular problem.
    >
    thanks for the reminder zsdc - i should have
    remembered to cc the list when replying to you.
    > bis wrote:
    >
    > > Tnaks zsdc and JEGII.
    > >
    > > --- zsdc <zsdc@wp.pl> wrote:
    > >
    > >>It gives a syntax error. Maybe try this:
    > >>
    > >> #!/usr/bin/perl -p
    > >> s/(\s)(.)/$1\u$2/g if/SCTN:/;
    > >
    > > This capitalises the first letter of every word in
    > the
    > > whole document.
    >
    > No, it doesn't. Only the lines containing "SCTN:"
    > Have you run it?
    yes i have run it and below is the kind of output i
    get (original input all lower case except capitalised
    tags and for SCTN: line which is mixed case):

    DOCB:
    SRCE: Marketing Week
    SCTN: Special Report:pRoMotIons And Incentives
    PGNO: 5
    HDLN: What
    TEXT:
    This Is Text This Is Text This Is Text These Are
    Double Spaces This Is Text This Is Text This Is Text
    These Are Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    DOCE:

    DOCB:
    SRCE: Marketing Week
    SCTN: Special Report:pRoMotIons And Incentives
    PGNO: 5
    HDLN: What
    TEXT:
    This Is Text This Is Text This Is Text These Are
    Double Spaces This Is Text This Is Text This Is Text
    These Are Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    This Is Text This Is Text This Is Text These Are
    Double Spaces
    DOCE:
    >
    > >>or instead of /SCTN:/ use /^SCTN:/
    > >
    > > This doesn't do anything.
    > >
    > > or /^\s*SCTN:/
    > >
    > > Nor does this.
    >
    > Well... Yes, it does. How did you run it, anyway?
    I have the script in the Perl/bin directory because it
    does not run if I have it anywhere else. Also I run it
    as #!/usr/bin/perl -w because #!/usr/bin/perl -p
    freezes my MS-DOS prompt.

    The original input is a file called test1.txt and the
    output is a file called test3.txt

    The whole program is as follows:

    #1. path to input and output file
    $inputfile = "C:/My Documents/Programming/Perl
    exercises/test/test1.txt";
    $outputfile = "C:/My Documents/Programming/Perl
    exercises/test/test3.txt";

    #2. filehandles
    open (INPUT, "$inputfile") || die "can't open
    $testfile";
    open (OUTPUT, ">$outputfile") || die "can't open
    $testfile";

    #initialise $text
    $text = "";

    #3. read input file into $text
    while (<INPUT>){
    $text = $text . $_;
    }

    # 4) split document by DOCE: string into array of
    tags
    @stories = split (/DOCB:/, $text);
    #@stories is array of each story

    for (@stories){
    # 5) general substitutions

    #Marketing Week substitutions
    if (/SRCE:\s*Marketing Week/i) {
    #put substitutions here

    s/(\s)(.)/$1\u$2/g if/SCTN:/; #uppercases everything

    # 6) now put back DOCE: tag into joined up array
    $text = join ("DOCB:", @stories)
    }

    # 7) now output file

    print OUTPUT $text;

    #close filehandles
    close (INPUT);
    close (OUTPUT);



    >
    > > I think the title of my query was misleading -
    > what I
    > > maybe should have said was "Uppercasing the first
    > > letter and lowercasing all the other letters of
    > every
    > > word in a mixed case part of a string"?
    >
    > This is much harder. It's easy to make the output
    > like this:
    >
    > I'Ve Been Reading O'Reilly Books
    >
    > OR:
    >
    > I've Been Reading O'reilly Books
    >
    > but not:
    >
    > I've Been Reading O'Reilly Books
    >
    > which is correct. I suggest you to only uppercase
    > characters, but if you
    > have to also lowercase the other ones then try:
    >
    > #!/usr/bin/perl -p
    > s/(\s)(\S+)/$1\L\u$2/g if/^\s*SCTN:/;
    >
    > or
    >
    > #!/usr/bin/perl -p
    > s/\b(\w+)/\L\u$1/g if/^\s*SCTN:/;
    >
    > (and maybe add:
    >
    > use locale;
    I get no result with any of this.
    >
    > at the beginning) and see which suits your needs
    > better. You might find
    > better examples in the Cookbook.
    Thank you. I'll take a look.

    -bis
    >
    > -zsdc.
    >
    __________________________________________________ ______________________
    Want to chat instantly with your online friends? Get the FREE Yahoo!
    Messenger [url]http://uk.messenger.yahoo.com/[/url]
    Bis Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139