Michael Stassen wrote:

> You are right that perl can do the search and replace in one line, but
> you'll need some more code to read and write the data, either from < > > mysql or from a source file before importing into mysql. That's why I > said "almost" a one-liner.
>
> You are also right that I got the perl line wrong. Considering how
> much perl I use, that's embarassing. Sigh. I have to admit I tested > the sql I suggested but not the perl. I should have been more careful.


I must have read your first post on this way too fast. I thought you were saying that perl couldn't do the search and replace in one line. After reading again I see that is not what you were saying.



>
> Your regular expression isn't quite right either, though.  The * means
> to match 0 or more,  so [a-z]* will match the start of ANY string.
> That's why you needed the $ at the end, as it anchors the search to
> match at the end.  Thus [a-z]*$ matches 0 or more lower case letters at
> the end.  This is fine if there are any lower case letters in the
> string, but will put a / at the end of any string that doesn't contain a
> lower case letter.  For example, it will turn "ABCD" into "ABCD/".  It
> will also turn "ABcdEFg" into "ABcdEF/g".   In other words, your version
> puts a / after the last non-lower case letter, instead of in front of
> the first lower case letter.


Thanks for the heads up on why the [a-z]* was doing what it was. I don't know why I didn't think of that.



> To find the first lower case letter, we need to look for [a-z]. So the > perl line should be > > $field =~ s/([a-z])/\/$1/ > > or, better yet > > $field =~ s|([a-z])|/$1| > > In vi, then, this would be > > :%s/[a-z]/\/&/

obviously your regexp will do what you are saying, but I'm not sure it is a best fit for what the original poster described.

He said that there would be some number of upper case letters followed with 2 or 3 lower case letters. He made no mention of there being any possibility of a mix of lower and upper case anywhere else. He also indicated that the lower case letters would always be at the end of the string so I am thinking that if you were going to assume the possibility of some other lower case letters in the sting you would not want to put the / in front of the first one you saw but only in front of a trailing 2 or 3 lower case letters so I think my first suggestion would be a better fit than the one you gust proposed. I also think the following would be a good fit, or maybe better than my first, since my first would place a / at the end of a line of all caps which shouldn't even exist if the data is as described by the original poster.

:%s/[a-z][a-z][a-z]*$/\/&/

that would only put the / in there if as described there were 2 or 3 lower case letters at the end of the string.

I love arguing about regular expressions... but maybe not the best topic for a MySQL list I guess we should take any further discussion on this off the list.

Chris W


-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]



Reply via email to