Michael Bahr
jedi at charm.net
Fri Sep 28 16:55:12 CDT 2007
John, you are correct that a regex would be a good choice however you did make the requirement that the first letter of each word had to be capitalized. Right?? So a regex really could not do this particular job very well if at all. Regular expressions are great for finding and manipulating patterns of characters in one felled swoop, for example if you only wanted to remove those pesky "extra" characters then something like the following would do the trick: Perl: my $line =~ s/:| |\%|\*|\$|\@|\!|\#|\&|^|_|-|,|\.//g; means to substitute all matching delimiters (g switch) with a nothing on the contents of $line. The s/pattern/replace/ expression is the construct. TCL: regsub -all {:| |\%|\*|\$|\@|\!|\#|\&|^|_|-|,|\.} $line "" line this may be a little more clear to that replace all matches found (-all switch) in $line with "" and put the new value in line. VB.net I believe: line = Regex.Replace(line, delimiters, "") The old way would be to let VB loop through each delimiter one at a time for each line whereas the regex engine does all the dirty work when you specify all the delimiters at one time. Make sense?? Mike... > Michael, > > The problem with doing this is that each object has to be examined > character > by character, EVERY line. In this specific case I could certainly do this > however I am under the impression that RegEx is optimized to do this kind > of > thing. Kind of the difference between a similar function to look at every > character and remove it if it matches something and using the replace to > remove a character. Replace is written in a highly optimized (perhaps even > in assembler) whereas the "loop and search" is not optimized at all. > > > > > John W. Colby > Colby Consulting > www.ColbyConsulting.com > -----Original Message----- > From: accessd-bounces at databaseadvisors.com > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Michael Bahr > Sent: Friday, September 28, 2007 2:25 PM > To: Access Developers discussion and problem solving > Subject: Re: [AccessD] Use Regex - Create Camel Case > > Hi John, here is one way to do it (although there are many ways to get the > same end result). Mind you this is air code but hopefully should be > enough > to get you going. You will need to create the main loop within your code. > > Create a list of all delimiters that are used in your CSV files such as > delimiters = '%|*|$|@|!|#|&|^|_|-|,|.|;|:| ' > > then run through your CSV files line by line evaluating the line saving > the > line into an array thisarray = Split(line, delimiters) > > then run through the array performing a Ucase on the first letter of each > word newline = "" > For item=1 to ubound > newline = newline & whatEverToCapFirstChar(item) Next item > > where ubound is the array size > > > Now here are two scripts that do the same thing, one is Perl and the other > is TCL. Both of these languages are open source and free and can be > gotten > at http://www.activestate.com/Products/languages.plex > > Perl: > > my $delimiters = '/:| |\%|\*|\$|\@|\!|\#|\&|^|_|-|,|\./'; > my @test = ("John colby", > "%idiotic_Field*name", > "hey#hey#Hey,hello_world", > "this#is_a_test_of_the-emergency-broadcast-system"); > > foreach my $item (@test) { > my $temp = ""; > my @list = split ($delimiters, $item); > foreach my $thing (@list) { > $temp .= ucfirst($thing); > } > print "$temp\n"; > > } > > Result > d:\Perl>pascalcase.pl > JohnColby > IdioticFieldName > HeyHeyHeyHelloWorld > ThisIsATestOfTheEmergencyBroadcastSystem > > TCL: > > set delimiters {%|*|$|@|!|#|&|^|_|-|,|.|;|:|\ "} set test [list {John > colby} > {%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] > set str "$str$s" > } > puts $str > > } > > Results > D:\VisualTcl\Projects>tclsh pascalcase.tcl JohnColby IdioticFieldName > HeyHeyHeyHelloWorld ThisIsATestOfTheEmergencyBroadcastSystem > > > hth, Mike... > > >> Folks, >> >> I am looking for a regex expression (preferably with explanation) for >> taking an expression and creating a camel case (or PascalCase) >> expression. >> >> I get CSV files with headers in them. All too often the eejits that >> created the databases they came from used embedded spaces or other >> special use characters (!@#$%^&* etc) in their field names. I need to >> strip these special characters out completely. I also need to upper >> case the valid alpha character that follows any of these special >> characters. >> >> John colby becomes JohnColby >> %idiotic_Field*name becomes IdioticFieldName >> >> Etc. >> >> It appears that Regex is the key (I am doing this in VB.Net) but until >> today I have never really tried to use RegEx and it ain't pretty! >> >> Any help in this would be much appreciated. >> >> John W. Colby >> Colby Consulting >> www.ColbyConsulting.com >> >> -- >> AccessD mailing list >> AccessD at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/accessd >> Website: http://www.databaseadvisors.com >> > > > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com > > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com >