[CS-FSLUG] regexp help

Tim Young Tim.Young at LightSys.org
Thu Nov 11 18:36:38 CST 2004


Yup.  I guess it does work.  Just goes to show there is a zillion ways to do it
with Linux tools.  Anyone want to try to find a command-line with less characters
than Frank's?

    - Tim Young

Frank Bax wrote:

> 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
>
> _______________________________________________
> ChristianSource FSLUG mailing list
> Christiansource at ofb.biz
> http://cs.uninetsolutions.com





More information about the Christiansource mailing list