[CS-FSLUG] regexp help

Frank Bax fbax at sympatico.ca
Thu Nov 11 17:45:15 CST 2004


At 05:10 PM 11/11/04, Tim Young wrote:
>Ahh...  I think Frank is a Perl or PHP guy... :)

Or both - I don't consider myself an expert in Perl, though - I'm 
constantly referring to the manual.

>Sed is a little "old-school" here and does not use the nifty regex nicities
>that Perl has built in.  The reason I added the ([a-Z0-9][a-Z0-9]*) with
>[a-Z0-9] twice is because the * after the second could match zero of them.  If
>there were just one of them ([a-Z0-9]*) it could match the following patterns:
>abcdef-
>-abcdef
>-
>and change them to
>abcdef--
>--abcdef
>--
>If he was looking to match ONLY:
>wordA-wordB
>
>You need at least one character before the - and one after.


Exactly!  I didn't suggest ([a-Z0-9]*)-([a-Z0-9]*), I suggested 
([a-Z0-9])-([a-Z0-9]) which matches exactly one alphanumeric character 
before and after the hyphen.  There is no need to match the rest of the 
word before and after the hyphen.  Can you tell I've done programming with 
limited memory available?

PS:  My system doesn't like a-Z and needs some extra slashes in the regexp:
$ cat myfile
worda - wordb
worda-
-wordb
-
worda-wordb
$ cat myfile | sed 
's/\([a-zA-Z0-9][a-zA-Z0-9]*\)-\([a-zA-Z0-9][a-zA-Z0-9]*\)/\1--\2/g'
worda - wordb
worda-
-wordb
-
worda--wordb
$ cat myfile | sed 's/\([a-zA-Z0-9]\)-\([a-zA-Z0-9]\)/\1--\2/g'
worda - wordb
worda-
-wordb
-
worda--wordb

Frank 





More information about the Christiansource mailing list