Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multiple substitution in a complex RE

Reply
Thread Tools

Multiple substitution in a complex RE

 
 
SqlSlinger
Guest
Posts: n/a
 
      02-18-2004
I'm trying to convert all occurances of variables of the form

VARIABLE_ONE, VARIABLE_NUMBER_TWO, a_really_long_VARIABLE_name

to the form

VariableOne, VariableNumberTwo, AReallyLongVariableName

Here's my Program:

$_='a_really_long_VARIABLE_name';
s/([A-Za-z])([^_ \t\n]*?)(_([A-Za-z0-9])([^_ \t\n]*?))+/\u$1\L$2\E\u$4\L$5\E/;
print;
__END__

Prints "AReally_long_VARIABLE_name".

Can anyone help?
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      02-18-2004
SqlSlinger <> wrote:
> I'm trying to convert all occurances of variables of the form
> VARIABLE_ONE, VARIABLE_NUMBER_TWO, a_really_long_VARIABLE_name
> to the form
> VariableOne, VariableNumberTwo, AReallyLongVariableName



my @vars = qw(VARIABLE_ONE VARIABLE_NUMBER_TWO a_really_long_VARIABLE_name);
my %StudlyCaps;
foreach (@vars) {
my $var = ucfirst lc;
$var =~ s/_(.)/\U$1/g;
$StudlyCaps{$_} = $var;
}
use Data:umper;
print Dumper(\%StudlyCaps);


--
Glenn Jackman
NCF Sysadmin

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      02-18-2004
>>>>> "GJ" == Glenn Jackman <> writes:


GJ> my @vars = qw(VARIABLE_ONE VARIABLE_NUMBER_TWO
GJ> a_really_long_VARIABLE_name);

GJ> foreach (@vars) {
GJ> my $var = ucfirst lc;
GJ> $var =~ s/_(.)/\U$1/g;
GJ> }

s/_?(.)([^_]*)/\U$1\L$2/g ;

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      02-18-2004
Uri Guttman <> wrote:
> >>>>> "GJ" == Glenn Jackman <> writes:

> GJ> my $var = ucfirst lc;
> GJ> $var =~ s/_(.)/\U$1/g;
>
> s/_?(.)([^_]*)/\U$1\L$2/g ;


Good one.

--
Glenn Jackman
NCF Sysadmin

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-19-2004
SqlSlinger wrote:
>
> I'm trying to convert all occurances of variables of the form
>
> VARIABLE_ONE, VARIABLE_NUMBER_TWO, a_really_long_VARIABLE_name
>
> to the form
>
> VariableOne, VariableNumberTwo, AReallyLongVariableName
>
> Here's my Program:
>
> $_='a_really_long_VARIABLE_name';
> s/([A-Za-z])([^_ \t\n]*?)(_([A-Za-z0-9])([^_ \t\n]*?))+/\u$1\L$2\E\u$4\L$5\E/;
> print;
> __END__
>
> Prints "AReally_long_VARIABLE_name".
>
> Can anyone help?



for ( qw/ VARIABLE_ONE VARIABLE_NUMBER_TWO a_really_long_VARIABLE_name / ) {
print;
print join '', map "\u\L$_", split /_/;
}



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
SqlSlinger
Guest
Posts: n/a
 
      02-19-2004
Glenn, Uri, John,

Excellent suggestions all!

Now here's the rest of what I should have told you. I use Perl every
chance I get, but in this case I'm hoping to program a macro in
TextPad to perform this kind of substitution. TextPad REs support
most of the Perl RE features, but not the g (global) modifier and not
loops. This is why I was attempting to get it all done in the s///
statement. If it won't work, of course, I can program a quick perl
script to do it using any of your suggestions, but that's slightly
more inconvenient than the TextPad macro.

For my own info, is there any way to get a repeated outer grouping in
the pattern to replace while referencing (and modifying) the inner
grouping in the replace string?
 
Reply With Quote
 
SqlSlinger
Guest
Posts: n/a
 
      02-19-2004
Uri Guttman <> wrote in message news:<>...
> >>>>> "GJ" == Glenn Jackman <> writes:

>
>
> GJ> my @vars = qw(VARIABLE_ONE VARIABLE_NUMBER_TWO
> GJ> a_really_long_VARIABLE_name);
>
> GJ> foreach (@vars) {
> GJ> my $var = ucfirst lc;
> GJ> $var =~ s/_(.)/\U$1/g;
> GJ> }
>
> s/_?(.)([^_]*)/\U$1\L$2/g ;
>
> uri


Nice and compact, but doesn't isolate only variables containing an
underscore. If run against a source file, would match any word,
right?

Vince
 
Reply With Quote
 
fifo
Guest
Posts: n/a
 
      02-19-2004
At 2004-02-19 05:37 -0800, SqlSlinger wrote:
> Now here's the rest of what I should have told you. I use Perl every
> chance I get, but in this case I'm hoping to program a macro in
> TextPad to perform this kind of substitution. TextPad REs support
> most of the Perl RE features, but not the g (global) modifier and not
> loops. This is why I was attempting to get it all done in the s///
> statement. If it won't work, of course, I can program a quick perl
> script to do it using any of your suggestions, but that's slightly
> more inconvenient than the TextPad macro.
>


If you need a single substitution to work over the entire file, how
about doing something like

s/ ([a-zA-Z0-9])([a-zA-Z0-9]*)(?=_[a-zA-Z0-9_])
| (?<=[a-zA-Z0-9_]_)([a-zA-Z0-9])([a-zA-Z0-9]*)
/\U$1$3\L$2$4/xg
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-19-2004
>>>>> "S" == SqlSlinger <> writes:

S> Uri Guttman <> wrote in message news:<>...
>> >>>>> "GJ" == Glenn Jackman <> writes:

>>
>>

GJ> my @vars = qw(VARIABLE_ONE VARIABLE_NUMBER_TWO
GJ> a_really_long_VARIABLE_name);
>>

GJ> foreach (@vars) {
GJ> my $var = ucfirst lc;
GJ> $var =~ s/_(.)/\U$1/g;
GJ> }
>>
>> s/_?(.)([^_]*)/\U$1\L$2/g ;
>>
>> uri


S> Nice and compact, but doesn't isolate only variables containing an
S> underscore. If run against a source file, would match any word,
S> right?

probably but that could be fixed with some guard regex stuff. and it
wasn't in the spec which showed a qw list of tokens. you need to code to
what is posted (unless it is obvious the OP is having an XY problem or
way off base).

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
SqlSlinger
Guest
Posts: n/a
 
      02-20-2004
fifo <> wrote in message news:<20040219143341.GA14410@fleece>...
> At 2004-02-19 05:37 -0800, SqlSlinger wrote:
> > Now here's the rest of what I should have told you. I use Perl every
> > chance I get, but in this case I'm hoping to program a macro in
> > TextPad to perform this kind of substitution. TextPad REs support
> > most of the Perl RE features, but not the g (global) modifier and not
> > loops. This is why I was attempting to get it all done in the s///
> > statement. If it won't work, of course, I can program a quick perl
> > script to do it using any of your suggestions, but that's slightly
> > more inconvenient than the TextPad macro.
> >

>
> If you need a single substitution to work over the entire file, how
> about doing something like
>
> s/ ([a-zA-Z0-9])([a-zA-Z0-9]*)(?=_[a-zA-Z0-9_])
> | (?<=[a-zA-Z0-9_]_)([a-zA-Z0-9])([a-zA-Z0-9]*)
> /\U$1$3\L$2$4/xg


Prints A_Really_Long_Variable_Name. Can we lose the underscores? I'm
not familiar with the RE directives you've used to be able to work up
an adjusted version, but I'll look into it. Doesn't seem to affect
text without the underscore, which is nice.

Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
Newbie question, schema, complex types and unordered multiple elemets davidjones@myself.com XML 3 10-17-2007 05:52 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments
 



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