Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!ogccse!littlei!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.unix.questions Subject: Re: a perl question Message-ID: <5192@omepd.UUCP> Date: 15 Nov 89 01:24:19 GMT References: Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: comp Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 41 In-reply-to: rjk@sawmill.uucp (Richard Kuhns) In article , rjk@sawmill (Richard Kuhns) writes: | I'm not entirely sure that this is the newsgroup I should use, but | I've seen a number of perl questions/answers and I don't know of a | better newgroup (until comp.lang.perl comes along). | | My question: I'd dearly love to have a filter, written in perl (the | rest of the code for this project is in perl, and I'll post it when I | get it working), which would turn the string `B^HBO^HOL^HLD^HD' into | `$bold_startBOLD$bold_end', where $bold_start and $bold_end are | predefined character strings. I have a filter that does this already | written in C, but it seems to me I should be able to do it easier in | perl (using regular expressions?), but I can't come up with a good way | to do it. /(.)\010$1/ recognizes one element of such a string (always | the first). s/(.)\010$1/$1/g specifically does NOT work (it only | changes the first occurence). I saw this question come through the perl-users@virginia.edu mailing list first, but I'll post my reply here (being the token Perl wizard...:-): #!/usr/bin/perl $bold_start = "whatever"; $bold_end = "whatever"; while (<>) { if (/\010/) { s/(.)\010\1/\201\1\202/g; # surround bold with \201 and \202 s/\202\201//g; # optimize away all end-start pairs s/\201/$bold_start/og; # replace start with real start s/\202/$bold_end/og; # and likewise for end } print; } There you have it. OK, so it's not a one-liner... big deal. Just another Perl hacker, (lwall says he's "Not just another Perl hacker"... :-) -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/