|
List Info
Thread: Regsub Problem
|
|
| Regsub Problem |
  United States |
2007-10-05 07:23:30 |
Hello, I have a need to remove delimiters from various
strings and
capitalize the first char following a delimiter match and
then recombine
everything into one string. My first attempt was to use
split to break
apart the string using a list of delimiters and save the
parts into a
list. Then run through the list of parts using totitle and
recombine
everything into a new string. All worked as planned, see
attempt 1.
Attempt 1;
set delimiters {%|*|$| |!|#|&|^|_|-|,|.|;|:| }
set test [list {thaddeus Cooper}
{%idiotic_Field*name}
{hey#hey#Hey,hello_world}
{this#is_a_test_of_the-emergency-broadcast-system}]
foreach item $test {
set str ""
set mylist [split $item, $delimiters]
foreach thing $mylist {
set s [string totitle $thing]
append str $s
}
puts $str
}
Result 1:
D:Projects>tclsh pascalcase.tcl
ThaddeusCooper
IdioticFieldName
HeyHeyHeyHelloWorld
ThisIsATestOfTheEmergencyBroadcastSystem
Wanting to improve upon this method I thought about using
regsub to do
all the dirty work. Attempt 2 shows my effort but does not
work
properly. I have 2 issues:
Issue 1: The variable $delimiters does not work properly
within the
regsub--just not matches, that is why I had to implicitly
hard-code the
pattern. Why?? I tried multiple escaping the chars and
still not
working.
Issue 2: The substitute part, [string toupper ] does
nothing. Why??
Attempt 2:
set delimiters {%|*|$| |!|#|&|^|_|-|,|.|;|:| }
set test [list {thaddeus Cooper}
{%idiotic_Field*name}
{hey#hey#Hey,hello_world}
{this#is_a_test_of_the-emergency-broadcast-system}]
foreach item $test {
regsub -all -- {(%|*|$| |!|#|&|^|_|-|,|.|;|:| )(w)} $item
[string toupper ] item1
puts $item1
}
Result 2:
D:Projects>tclsh pascalcase.tcl
thaddeusCooper
idioticFieldname
heyheyHeyhelloworld
thisisatestoftheemergencybroadcastsystem
Now to make this more interesting I did the same thing in
Perl and it
works as expected.
my $delimiters = '/:| |%|*|$| |!|#|&|^|_|-|,|./';
my test = ("thaddeus Cooper ",
"%idiotic_Field*name",
"hey#hey#Hey,hello_world",
"this#is_a_test_of_the-emergency-broadcast-system"
);
foreach my $item ( test) {
$item =~ s/
|%|*|$| |!|#|&|^|_|-|,|.)(w)/ucfirst($2)/eg;
print "$itemn";
}
Result:
D: Perl>pascalcase.pl
ThaddeusCooper
IdioticFieldName
HeyHeyHeyHelloWorld
ThisIsATestOfTheEmergencyBroadcastSystem
Thanks, Mike...
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
| Re: Regsub Problem |

|
2007-10-05 08:08:21 |
On 2007.10.05, Bahr, Michael <mbahr hst.nasa.gov> wrote:
> set delimiters {%|*|$| |!|#|&|^|_|-|,|.|;|:| }
> set test [list {thaddeus Cooper}
> {%idiotic_Field*name}
> {hey#hey#Hey,hello_world}
>
{this#is_a_test_of_the-emergency-broadcast-system}]
>
> foreach item $test {
> regsub -all -- {(%|*|$| |!|#|&|^|_|-|,|.|;|:| )(w)} $item
> [string toupper ] item1
> puts $item1
> }
>
> Result 2:
> D:Projects>tclsh pascalcase.tcl
> thaddeusCooper
> idioticFieldname
> heyheyHeyhelloworld
> thisisatestoftheemergencybroadcastsystem
This is the simplest way of doing this that I can come up
with:
set test {
{thaddeus Cooper}
{%idiotic_Field*name}
{hey#hey#Hey,hello_world}
{this#is_a_test_of_the-emergency-broadcast-system}
}
set re {[^-%*$ !#&^_,.;: ]+}
foreach item $test {
set new {}
foreach match [regexp -all -inline -- $re $item] {
append new [string totitle $match]
}
puts $new
}
HTH, HAND,
-- Dossy
--
Dossy Shiobara | dossy panoptic.com | http://dossy.org/
Panoptic Computer Network | http://panoptic.com/
"He realized the fastest way to change is to laugh at
your own
folly -- then you can let go and quickly move on."
(p. 70)
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
| Re: Regsub Problem |
  United States |
2007-10-05 08:50:25 |
Thanks Dossy, I get the impression that Tcl's regsub does
not support
commands, i.e. [ ], for the "replacement" string.
So is this a bug or
feature not implemented?
Thanks, Mike...
> -----Original Message-----
> From: activetcl-bounces listserv.ActiveState.com
> [mailto:activetcl-bounces listserv.ActiveState.com]
On Behalf
> Of Dossy Shiobara
> Sent: Friday, October 05, 2007 9:08 AM
> To: activetcl listserv.ActiveState.com
> Subject: Re: [Activetcl] Regsub Problem
>
> On 2007.10.05, Bahr, Michael <mbahr hst.nasa.gov> wrote:
> > set delimiters {%|*|$| |!|#|&|^|_|-|,|.|;|:| } set test
> > [list {thaddeus Cooper}
> > {%idiotic_Field*name}
> > {hey#hey#Hey,hello_world}
> >
{this#is_a_test_of_the-emergency-broadcast-system}]
> >
> > foreach item $test {
> > regsub -all -- {(%|*|$| |!|#|&|^|_|-|,|.|;|:| )(w)}
> > $item [string toupper ] item1
> > puts $item1
> > }
> >
> > Result 2:
> > D:Projects>tclsh pascalcase.tcl
> > thaddeusCooper
> > idioticFieldname
> > heyheyHeyhelloworld
> > thisisatestoftheemergencybroadcastsystem
>
> This is the simplest way of doing this that I can come
up with:
>
> set test {
> {thaddeus Cooper}
> {%idiotic_Field*name}
> {hey#hey#Hey,hello_world}
>
{this#is_a_test_of_the-emergency-broadcast-system}
> }
>
> set re {[^-%*$ !#&^_,.;: ]+}
> foreach item $test {
> set new {}
> foreach match [regexp -all -inline -- $re
$item] {
> append new [string totitle $match]
> }
> puts $new
> }
>
> HTH, HAND,
>
> -- Dossy
>
> --
> Dossy Shiobara | dossy panoptic.com | http://dossy.org/
> Panoptic Computer Network | http://panoptic.com/
> "He realized the fastest way to change is to
laugh at your own
> folly -- then you can let go and quickly move
on." (p.
> 70) _______________________________________________
> ActiveTcl mailing list
> ActiveTcl listserv.ActiveState.com
> To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
| Re: Regsub Problem |
  United States |
2007-10-05 12:42:36 |
Hi Mike. I have used the 2-step "subst" technique
of page 141 in 4th
edition of the Welch book to do "command
substitution" (pun intended).
Is this what you're looking for? If you're running into
trouble with
that, it's probably the "backslash hell" that is
often needed in either
the pattern or the replacement that is fouling things up. I
tend to be
empirical about those until I can get them to work.
set data {John T. Smith}
set middle "T."
set halfway [regsub $middle $data "[string tolower
\0]"]
set final [subst $halfway]
% John T. Smith
% T.
% John [string tolower T.] Smith
% John t. Smith
%
-Erik
-----Original Message-----
From: activetcl-bounces listserv.activestate.com
[mailto:activetcl-bounces listserv.activestate.com]
On Behalf Of Bahr,
Michael
Sent: Friday, October 05, 2007 9:50 AM
To: Dossy Shiobara; activetcl listserv.activestate.com
Subject: Re: [Activetcl] Regsub Problem
Thanks Dossy, I get the impression that Tcl's regsub does
not support
commands, i.e. [ ], for the "replacement" string.
So is this a bug or
feature not implemented?
Thanks, Mike...
> -----Original Message-----
> From: activetcl-bounces listserv.ActiveState.com
> [mailto:activetcl-bounces listserv.ActiveState.com]
On Behalf
> Of Dossy Shiobara
> Sent: Friday, October 05, 2007 9:08 AM
> To: activetcl listserv.ActiveState.com
> Subject: Re: [Activetcl] Regsub Problem
>
> On 2007.10.05, Bahr, Michael <mbahr hst.nasa.gov> wrote:
> > set delimiters {%|*|$| |!|#|&|^|_|-|,|.|;|:| } set test
> > [list {thaddeus Cooper}
> > {%idiotic_Field*name}
> > {hey#hey#Hey,hello_world}
> >
{this#is_a_test_of_the-emergency-broadcast-system}]
> >
> > foreach item $test {
> > regsub -all -- {(%|*|$| |!|#|&|^|_|-|,|.|;|:| )(w)}
> > $item [string toupper ] item1
> > puts $item1
> > }
> >
> > Result 2:
> > D:Projects>tclsh pascalcase.tcl
> > thaddeusCooper
> > idioticFieldname
> > heyheyHeyhelloworld
> > thisisatestoftheemergencybroadcastsystem
>
> This is the simplest way of doing this that I can come
up with:
>
> set test {
> {thaddeus Cooper}
> {%idiotic_Field*name}
> {hey#hey#Hey,hello_world}
>
{this#is_a_test_of_the-emergency-broadcast-system}
> }
>
> set re {[^-%*$ !#&^_,.;: ]+}
> foreach item $test {
> set new {}
> foreach match [regexp -all -inline -- $re
$item] {
> append new [string totitle $match]
> }
> puts $new
> }
>
> HTH, HAND,
>
> -- Dossy
>
> --
> Dossy Shiobara | dossy panoptic.com | http://dossy.org/
> Panoptic Computer Network | http://panoptic.com/
> "He realized the fastest way to change is to
laugh at your own
> folly -- then you can let go and quickly move
on." (p.
> 70) _______________________________________________
> ActiveTcl mailing list
> ActiveTcl listserv.ActiveState.com
> To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
| Re: Regsub Problem |
  United States |
2007-10-05 13:18:01 |
> Wanting to improve upon this method I thought about
using regsub to do
all the dirty work.
Let me boil down what you were trying to get to work.
In perl, one can process a regular expression, then run
functions
against back references. So you wanted to be able to do
something like
that:
regsub -all -- {(%|*|$| |!|#|&|^|_|-|,|.|;|:| )(w)} $item
[string toupper ] item1
However, that string toupper function occurs before regsub
ever is
called. In tcl, you would have to do something
different...
Right now, that regsub is returning a counnt. What you need
is a tcl
legal list returned. Then you could perhaps use Tcl 8.5's
apply function
to apply the string toupper to the returned list...
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
| Re: Regsub Problem |

|
2007-10-05 14:34:58 |
On 2007.10.05, Bahr, Michael <mbahr hst.nasa.gov> wrote:
> Thanks Dossy, I get the impression that Tcl's regsub
does not support
> commands, i.e. [ ], for the "replacement"
string. So is this a bug or
> feature not implemented?
At best, it's a feature not implemented. It's certainly not
a bug, by
any means.
If you can show that there's an appreciable performance
impact to Tcl's
approach of not cramming the ability to eval every
substitution
parameter--I mean, that Tcl's way of doing things is
measurably
slower--then it's a feature request. It's not a bug because
I haven't
seen anywhere in Tcl's documentation that it was ever a
feature in the
first place.
People's exposure to Perl has a dangerous side-effect:
people mistakenly
form the belief that slightly more verbose code is
"worse" or that it
performs less well. (To reverse this: just because it's a
more densely
compact expression doesn't make it "more
efficient" or faster.)
-- Dossy
--
Dossy Shiobara | dossy panoptic.com | http://dossy.org/
Panoptic Computer Network | http://panoptic.com/
"He realized the fastest way to change is to laugh at
your own
folly -- then you can let go and quickly move on."
(p. 70)
_______________________________________________
ActiveTcl mailing list
ActiveTcl listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|
|
[1-6]
|
|