简体   繁体   English

使用TRegEx从字符串中去除{…}或[…]

[英]Strip {…} or […] from string using TRegEx

I have the following functions, which should remove all occurrences of (...), [...] and {...} in a string 我有以下函数,应删除字符串中所有出现的(...),[...]和{...}

function TCleanUp.DoStripBraces(const aInput: string): string; // works!
begin
  result := TRegEx.Replace(aInput, '\([^)]*\)', '');
end;

function TCleanUp.DoStripCurlyBraces(const aInput: string): string; // does not work
begin
  result := TRegEx.Replace(aInput, '\{[^\}]*}', '');
end;

function TCleanUp.DoStripSquareBrackets(const aInput: string): string; // does not work
begin
  result := TRegEx.Replace(aInput, '\[[^\]]*]', '');
end;

I'm testing the functions with these strings 我正在用这些字符串测试功能

'foo (bar) baz (xyz)'
'foo [bar] baz [xyz]'
'foo {bar} baz {xyz}'

which all should return the following string 所有这些都应该返回以下字符串

'foo  baz '

When I use the same strings and expressions on http://www.regexr.com/ it matches the occurrences perfectly. 当我在http://www.regexr.com/上使用相同的字符串和表达式时,它完全匹配出现的情况。

I also tried to not escape the bracket / curly brace in the character set, but that did not work either. 我也尝试过不使字符集中的括号/花括号转义,但这也不起作用。

How can I make the expressions work? 如何使表达式起作用?

You could use a one regex like this: 您可以这样使用一个正则表达式:

[([{].*?[)}\]]

Working demo 工作演示

On the other hand, if you want to have 3 separated regex you can use: 另一方面,如果要使用3个分隔的正则表达式,可以使用:

\(.*?\)
\[.*?\]
\{.*?\}

Putting them altogether, you can see what they match: 放在一起,您可以看到它们的匹配:

正则表达式可视化

These regexes above are more readable than have: 上面的这些正则表达式比以下内容更具可读性:

\([^)]*?\)     As you can see, this is error prone as you faced. 
\[[^\]]*?\]
\{[^}]*?\}

Although, the disadvantage of this readability impacts a little on the performance. 虽然,这种可读性的缺点对性能有一点影响。 Using .*? 使用.*? is slower than use [^...]* but unless you have to parse really long strings you won't notice the difference. 比使用[^...]*慢,但是除非必须解析非常长的字符串,否则您不会注意到其中的区别。

You can see the difference visually: 您可以从视觉上看到差异:

正则表达式可视化

You forgot to escape the latest occurrences of ] and } . 您忘记逃脱了最近出现的]}

Try '\\{[^\\}]*\\}' and '\\[[^\\]]*\\]' . 尝试使用'\\{[^\\}]*\\}''\\[[^\\]]*\\]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM