我以前从未使用过 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/