🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.tcl
Article #347

Re: regexp bez podsłów

#347
From: Wojciech Kocjan
Date: Mon, 27 Feb 2006 18:20
43 lines
2061 bytes
On Mon, 27 Feb 2006 19:09:36 +0100, Googie <no@spam.org> wrote:

> Wojciech Kocjan wrote:
>> Chyba chodziło o \s - czyli "\\sbb\\s" w tym przypadku :)
>
> Tylko że wtedy zamieni " xx " na "bb" = utnie spacje/taby. Poza tym  
> koctyxa napisał że chodzi też o '()', itp. Z tego na ile znam regexpa w  
> Tcl, to nie ma takiego operatora, który spełniałby te wymogi, ale moge  
> się mylić.

No tak, ale może coś tego typu:

proc escapeRegexp {string} {
     set escape "\[\]\{\}\(\)\\.+-*"
     set rc ""
     foreach char [split $string ""] {
         if {[string first $char $escape] >= 0} {
             append rc \\
         }
         append rc $char
     }
     return $rc
}

set from "xx"
set to "bb"
set text "ala ma xx kota xxy xx"

regsub -all "(^|\\s)[escapeRegexp $from](\\s|\$)" $text "\\1${to}\\2"

Jest też inna możliwość.

set word(xx) bb

set rc ""
foreach {- before word after} [regexp -all -inline \
     "(^|\\s)(\\w*?)(\\s|\$)" $text] {
     catch {set word $replace($word)}
     append rc $before $word $after
}

-- 
WK
Technical Details
Message-ID: <op.s5m872gg7lonqf@hesus.mshome.net>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.internetia.pl!newsfeed.tpinternet.pl!nemesis.news.tpi.pl!atlantis.news.tpi.pl!news.tpi.pl!not-for-mail
References: <op.s5mlego53jrrtc@bmw-ws076.bqeu.corp.com> <op.s5m7wzjw7lonqf@hesus.mshome.net> <dtvel5$2q6$1@srv.cyf-kr.edu.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.internetia.pl!newsfeed.tpinternet.pl!nemesis.news.tpi.pl!atlantis.news.tpi.pl!news.tpi.pl!not-for-mail
From: "Wojciech Kocjan" <hceicjow.backward@kocjan.org>
Newsgroups: pl.comp.lang.tcl
Subject: Re: regexp bez podsłów
Date: Mon, 27 Feb 2006 19:20:16 +0100
Organization: tp.internet - http://www.tpi.pl/
Lines: 43
Message-ID: <op.s5m872gg7lonqf@hesus.mshome.net>
References: <op.s5mlego53jrrtc@bmw-ws076.bqeu.corp.com> <op.s5m7wzjw7lonqf@hesus.mshome.net> <dtvel5$2q6$1@srv.cyf-kr.edu.pl>
NNTP-Posting-Host: ablz73.neoplus.adsl.tpnet.pl
Mime-Version: 1.0
X-Trace: nemesis.news.tpi.pl 1141064506 24652 83.7.219.73 (27 Feb 2006 18:21:46 GMT)
X-Complaints-To: usenet@tpi.pl
NNTP-Posting-Date: Mon, 27 Feb 2006 18:21:46 +0000 (UTC)
User-Agent: Opera M2/8.51 (Win32, build 7712)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit