【问题标题】:TRegEx Replace Pattern?TRegEx 替换模式?
【发布时间】:2013-04-03 11:57:39
【问题描述】:

我有一个json数据

  "myObject": {
      "field1": 1,
      "field2": true,
      "fileURL": [ "" ]
   }

如何替换成 "fileURL":["here url"] ?

var
 pattern:string;
begin
 pattern:='"fileURL":[ "?" ]';
 Memo1.Text:=TRegEx.Replace(Trim(Memo1.Text),pattern,'C:\file1.doc');
end;

【问题讨论】:

  • 我不是正则表达式方面的专家,但您可以尝试摆弄this pattern。或者只使用 JSON 解析器。
  • 在没有解析器的情况下使用 JSON 就像买了一辆车然后推到你的目的地

标签: regex delphi replace delphi-xe2


【解决方案1】:

用 [a-zA-Z0-9.-_]+ 替换您的问号 并且不要忘记您想要精确重复的所有字符的反斜杠 \ & 你应该用括号 () 来表示组创建,并且在替换部分你可以使用它。

var
 pattern:string;
begin
 pattern:='(\"fileURL\"\:\[\s\")([a-zA-Z0-9\.\-\_]+)(\"\s\])';
 Memo1.Text:=TRegEx.Replace(Trim(Memo1.Text),pattern,'$1C:\file1.doc$3');
end;

【讨论】:

【解决方案2】:

我会替换整行。

pattern := '"fileURL"\s*:\s*\[\s*"[^"]*"\s*\]';
fileName := 'C:\file1.doc';
Memo1.Text := TRegEx.Replace(Memo1.Text, pattern, '"fileURL" : ["' + fileName + '"]');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多