LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2006, 01:29 PM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
Capitalization Perl Regex


What's a perl regex that will take [[*]], where * is a sentence with indeterminate spacing and capitalize all the words in the sentence, and surround it by brackets again?
Example:
[[a string with Indeterminate spacing]] to
[[A String With Indeterminate Spacing]]

I tried s/\[\[\b(\w)\]\]/[[uc($1)]]/eg
but it didn't work.
Can someone also tell me where I can find what perl extensions like /e does? Thanks.
 
Old 10-27-2006, 01:35 PM   #2
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559

Rep: Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106
Why not just
Code:
uc("[[a string with Indeterminate spacing]]");
Eric
 
Old 10-27-2006, 01:44 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
s/\w+)/\u\L$1/g;



Cheers,
Tink
 
Old 10-27-2006, 05:36 PM   #4
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Original Poster
Rep: Reputation: 15
That capitalizes all the words in a sentence

...but I only want to do it for sentences inside brackets.
s/\[\[(\w)+\]\]/[[\u$1]]/g didn't work for:
[[these thrEE words]], which should have transformed it to [[These ThrEE Words]].
Instead, it doesn't do anything. It does transform [[thrEE]] to [[ThrEE]] though.
 
Old 10-28-2006, 09:38 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I would approach this as first identifying the appropriate lines. Once I had a line with "[[...]]" in it, I would split apart the stuff before and after the interesting section, namely the string between the brackets.

For example, putting code with data around your idea:
Code:
#!/usr/bin/perl

# @(#) p4       Demonstrate uc on isolated string.

use warnings;
use strict;

my($t1,$t2,$t3);

while ( <DATA> ) {
        chomp;
        # old pattern -- s/\[\[\b(\w)\]\]/[[uc($1)]]/eg;
        # Check for [], isolate three parts, interested in $2.
        #         1     2     3
        #      v      vv  vv      v
        if ( /^(.*\[\[)(.*)(\]\].*)$/ ) {
                $t1 = $1; $t2 = $2; $t3 = $3;
                $t2 =~ s/\b(\w)/uc $1/eg;
                print " title after  :$t2:\n";
                print "$t1$t2$t3\n";
        } else {
                print " No match for :$_:\n";
        }
        print "\n";
}
__DATA__
No brackets.
[[simple string]]
Junk before [[a string with Indeterminate spacing within brackets]] ja.
[[ one 123 two ]]
[[]]
which produces:
Code:
% ./p4
 No match for :No brackets.:

 title after  :Simple String:
[[Simple String]]

 title after  :A String With Indeterminate Spacing Within Brackets:
Junk before [[A String With Indeterminate Spacing Within Brackets]] ja.

 title after  : One 123 Two :
[[ One 123 Two ]]

 title after  ::
[[]]
If you don't understand something here, do some intermediate prints to see what is going on.

Best wishes ... cheers, makyo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl + regex issues. kurrupt Programming 2 10-11-2006 09:33 AM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
simple perl and regex phlx Programming 6 12-03-2004 03:01 PM
HELP ->PERL regex is kick'n my a... prisoner Programming 2 03-18-2004 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration