C#正则表达式:删除前导和尾随双引号(“)

时间:2021-06-03 22:33:02

If I have a string like below... what is the regular expression to remove the (optional) leading and trailing double quotes? For extra credit, can it also remove any optional white space outside of the quotes:

如果我有一个像下面的字符串...什么是正则表达式删除(可选)前导和尾随双引号?对于额外的功劳,它还可以删除引号之外的任何可选空格:

string input = "\"quoted string\""   -> quoted string
string inputWithWhiteSpace = "  \"quoted string\"    "  => quoted string

(for C# using Regex.Replace)

(对于使用Regex.Replace的C#)

5 个解决方案

#1


32  

It's overkill to use Regex.Replace for this. Use Trim instead.

使用Regex.Replace来做这件事太过分了。请改用Trim。

string output = input.Trim(' ', '\t', '\n', '\v', '\f', '\r', '"');

And if you only want to remove whitespace that's outside the quotes, retaining any that's inside:

如果你只想删除引号之外的空格,保留其中的任何内容:

string output = input.Trim().Trim('"');

#2


9  

Besides using a regular expression you can just use String.Trim() - much easier to read, understand, and maintain.

除了使用正则表达式之外,您还可以使用String.Trim() - 更容易阅读,理解和维护。

var result = input.Trim('"', ' ', '\t');

#3


3  

Replace ^\s*"?|"?\s*$ with an empty string.

用空字符串替换^ \ s *“?|”?\ s * $。

In C#, the regex would be:

在C#中,正则表达式将是:

string input = "  \"quoted string\"    "l
string pattern = @"^\s*""?|""?\s*$";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, "");
Console.WriteLine(result);

#4


1  

I would use String.Trim method instead, but if you want regex, use this one:

我会使用String.Trim方法,但如果你想要正则表达式,请使用以下方法:

@"^(\s|")+|(\s|")+$"

#5


1  

I created a slightly modified version of another pattern that works pretty well for me. I hope this helps for separating normal command-line parameters and double-quoted sets of words that act as a single parameter.

我创建了另一个模式的略微修改版本,对我来说效果很好。我希望这有助于分离正常的命令行参数和作为单个参数的双引号词集。

String pattern = "(\"[^\"]*\"|[^\"\\s]+)(\\s+|$)";

#1


32  

It's overkill to use Regex.Replace for this. Use Trim instead.

使用Regex.Replace来做这件事太过分了。请改用Trim。

string output = input.Trim(' ', '\t', '\n', '\v', '\f', '\r', '"');

And if you only want to remove whitespace that's outside the quotes, retaining any that's inside:

如果你只想删除引号之外的空格,保留其中的任何内容:

string output = input.Trim().Trim('"');

#2


9  

Besides using a regular expression you can just use String.Trim() - much easier to read, understand, and maintain.

除了使用正则表达式之外,您还可以使用String.Trim() - 更容易阅读,理解和维护。

var result = input.Trim('"', ' ', '\t');

#3


3  

Replace ^\s*"?|"?\s*$ with an empty string.

用空字符串替换^ \ s *“?|”?\ s * $。

In C#, the regex would be:

在C#中,正则表达式将是:

string input = "  \"quoted string\"    "l
string pattern = @"^\s*""?|""?\s*$";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, "");
Console.WriteLine(result);

#4


1  

I would use String.Trim method instead, but if you want regex, use this one:

我会使用String.Trim方法,但如果你想要正则表达式,请使用以下方法:

@"^(\s|")+|(\s|")+$"

#5


1  

I created a slightly modified version of another pattern that works pretty well for me. I hope this helps for separating normal command-line parameters and double-quoted sets of words that act as a single parameter.

我创建了另一个模式的略微修改版本,对我来说效果很好。我希望这有助于分离正常的命令行参数和作为单个参数的双引号词集。

String pattern = "(\"[^\"]*\"|[^\"\\s]+)(\\s+|$)";