regex - 如何在 Delphi 上创建正则表达式以删除括号和引号

标签 regex delphi

我以前从未使用过 TPerlRegEx,这是我第一次使用正则表达式。

我正在寻找一个在 Delphi Xe2 中使用 TPerlRegEx 删除括号和引号的小示例,如下所示:

输入字符串:

["some text"]

结果:

some text

单行,没有嵌套的括号或引号。我使用 Regexbuddy 来创建和测试正则表达式,但它没有给我结果。

最佳答案

这在 Regex Buddy 中有效:

正则表达式:

\["(.+?)"\]

替换:

$1

像这样使用:

var
  RegEx: TPerlRegEx;
begin
  RegEx := TPerlRegEx.Create(nil);
  try
    Regex.RegEx := '\["(.+?)"\]';
    Regex.Subject := SubjectString;  // ["any text between brackets and quotes"]
    Regex.Replacement := '$1';
    Regex.ReplaceAll;
    Result := Regex.Subject;
  finally
    RegEx.Free;
  end;
end;

工作原理:

Match the character "[" literally «\[»
Match the character """ literally «"»
Match the regular expression below and capture its match into backreference number 1 «(.+?)»
   Match any single character that is not a line break character «.+?»
      Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
Match the character """ literally «"»
Match the character "]" literally «\]»


Created with RegexBuddy

关于regex - 如何在 Delphi 上创建正则表达式以删除括号和引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941001/

相关文章:

java - 如何从我的正则表达式中删除这个字符?

ruby - Ruby 和 Perl 中的 Regex 语法有什么区别?

javascript - 正则表达式匹配仅包含一个空格的名称

php - 密码字段正则表达式

regex - 从 mac 上的 bash 中的 xml 中提取值

delphi - 将配置传递给与应用程序无关的模块的最佳方式是什么?

delphi - 我可以从命令行构建 Delphi 多语言项目吗?

sql - 使用 Fields[0].Value 从 FOR XML RAW 获取 XML,ELEMENTS 查询困惑

delphi - 取消后重用 ADOQuery

delphi - Delphi XE2 Listview工件