有没有通用的代码,来实现:复杂的替换?

haitao 2012-12-17 03:34:41
除了正则表达式

例如:把
abc{*1}123{*2}xzy
替换为
987{*2}nm{*1}efg
({*n}指任意内容,n为顺序代码)

即:遇到
abcewrihfkshdkf12397945xzy
会替换成
98797945nmewrihfkshdkfefg
(颜色只是为了突出,实际处理的是纯文本内容)

或者,如何把上面的需求 自动转为 正则表达式 能识别的?
...全文
128 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
haitao 2012-12-18
  • 打赏
  • 举报
回复
非常好用! 以前下过一个:不知道与perl这个有什么差别 TRegExpr Delphi/Kylix/FreePascal library Easy to use and powerfull tool for sophisticated search and substitutioning and for template-based text checking (especially usefull for user input validation in DBMS and web projects). You can validate e-mail adresses, extract phone numbers or ZIP-codes from web-pages or documents, search for complex patterns in log files and all You can imagine! Rules (templates) can be changed without Your program recompilation! As a language for rules used subset of Perl's regular expressions (regexp). Full source codes included, pure Object Pascal. Thus, You need no DLL! Documentation in English, Russian, German, Bulgarian, French and Spanish available at TRegExpr home page http://anso.da.ru/ or http://anso.virtualave.net/ (c) 2004 Andrey V. Sorokin mailto:anso@mail.ru Saint Petersburg, Russia
haitao 2012-12-17
  • 打赏
  • 举报
回复
哦,谢谢,明天试一试 正则的语法实在有点晕 是基于perl的? 还有nginx的config也是,看得非常累
s11ss 2012-12-17
  • 打赏
  • 举报
回复
纠错: p.Subject := S;//abcewrihfkshdkf12397945xzy 注释应当是两个abcewrihfkshdkf12397945xzy。。。
s11ss 2012-12-17
  • 打赏
  • 举报
回复
//uses PerlRegEx; function StringReplaceEx(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; var p: TPerlRegEx; begin p := TPerlRegEx.Create; try p.RegEx := '\{\*([1-9][0-9]*)\}'; p.Subject := OldPattern; p.Replacement := '(.*?)'; p.ReplaceAll; Result := p.Subject; p.Subject := NewPattern; p.Replacement := '\\\1'; p.ReplaceAll; p.RegEx := Result;//abc(.*?)123(.*?)xzy p.Replacement := p.Subject;//987\2nm\1efg p.Subject := S;//abcewrihfkshdkf12397945xzy p.Options := p.Options + [preSingleLine]; if rfIgnoreCase in Flags then p.Options := p.Options + [preCaseLess]; if rfReplaceAll in Flags then p.ReplaceAll else if p.Match then p.Replace; Result := p.Subject; finally p.Free end; end; procedure TForm1.FormCreate(Sender: TObject); begin ShowMessage(StringReplaceEx('abcewrihfkshdkf12397945xzyabcewrihfkshdkf12397945xzy', 'abc{*1}123{*2}xzy', '987{*2}nm{*1}efg', [rfReplaceAll])) end;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧