简体   繁体   English

如何使用正则表达式从字符串(如果有)中删除最后一个逗号和空格?

[英]How to remove the last comma and space from string (if there) using regex?

我在C#应用程序中有一个字符串,并想知道检查最后两个字符(逗号和空格)的正确方法是什么,并使用正则表达式删除它们(如果是这样)。

A working solution without regex : 没有正则表达式的工作解决方案:

string str = "dfsf, ";

if (str.EndsWith(", "))
    str = str.Remove(str.Length - 2);

We remove 2 because 2 is the length of ", " . 我们删除2因为2是", "的长度。

demo 演示

You don't need Regex for that, use String.TrimEnd like: 你不需要Regex,使用String.TrimEnd如:

string updatedString = yourString.TrimEnd(',', ' ');

You can also specify a string and call ToCharArray like: 您还可以指定一个字符串并调用ToCharArray如:

string str = "something,  ,   ,,,,   ";
string updatedString = str.TrimEnd(", ".ToCharArray());

would give you "something" 会给你"something"

If you only want to remove a single occurrence of ", " (comma and space at the end) then use: 如果您只想删除单个 ", " (最后的逗号和空格),请使用:

if (str.EndsWith(", "))
    updatedString = str.Substring(0, str.Length - 2);

Rather than removing the trailing comma and space it's easier to simply generate the string without an extra comma in the first place. 不是删除尾随的逗号和空格,而是首先简单地生成字符串而不需要额外的逗号。 When generating the comma separated values simply use String.Join to join all of the strings together with a particular separator: 生成逗号分隔值时,只需使用String.Join将所有字符串与特定分隔符连接在一起:

var line = string.Join(", ", yourCollectionOfValues);

It'll be easier and faster than appending the values together, and Join will already handle ensure that there is no trailing separator. 它比将值附加在一起更容易,更快, Join已经处理,确保没有尾随分隔符。

(Sorry I was late the the party.) (对不起,我迟到了。)

Yes , this is not something for which you have to or should use a regex; 是的 ,这不是你必须或应该使用正则表达式的东西; but since you asked how to do it with a regex (eg maybe you are just curious, and "suppose I have to to this with a regex" hypotheticals are a good way to learn), consider the following pattern: 但是既然你问过如何使用正则表达式(例如,也许你只是好奇,并且“假设我必须使用正则表达式”假设是一种很好的学习方法),请考虑以下模式:

(.*?)(, )?$

You can test it in a related regex fiddle . 您可以在相关的正则表达式中进行测试。

Key points: 关键点:

  • (.*?) – Match zero or more ( * ) of any character except newline ( . ) as few times as possible ( ? ). (.*?) - 尽可能少的几次? )匹配除换行符( . )之外的任何字符的零或多( * )。
  • (, )?$ – Match zero or one ( ? ) , at the end ( $ ). (, )?$ - 匹配零或一( ?, 结尾$ )。

Also, the following C# example with this pattern... 此外,以下C#示例使用此模式...

var str1 = "whatever, ";
var str2 = "whatever, ,";
var str3 = "";

var regex = new Regex("(.*?)(, )?$");
var str1Match = regex.Match(str1);
var str2Match = regex.Match(str2);
var str3Match = regex.Match(str3);

Console.WriteLine(str1Match.Groups[1].Value);
Console.WriteLine(str2Match.Groups[1].Value);
Console.WriteLine(str3Match.Groups[1].Value);

...produces the following outputs: ...产生以下输出:

  • str1 ( "whatever, " ) => whatever str1"whatever, "=> whatever
  • str2 ( "whatever, ," ) => whatever, , str2"whatever, ,"=> whatever, ,
  • str3 ( "" ) => str3""=>

It uses Groups[1].Value to get the first capture group's value (ie the value matched by (.*?) rather than (, )? (if any)). 它使用Groups[1].Value来获取第一个捕获组的值(即匹配的值(.*?)而不是(, )? (如果有的话))。

Edit: 编辑:

I actually like what @UlugbekUmirov suggested in his comment ( string output = Regex.Replace("my string, ", ", $", ""); ) even better because it is super simple; 我实际上喜欢@UlugbekUmirov在他的评论中提出的建议( string output = Regex.Replace("my string, ", ", $", ""); )甚至更好,因为它非常简单; but hopefully you find the approach I outlined instructive. 但希望你发现我概述的方法很有启发性。

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

相关问题 如何使用正则表达式验证带空格的逗号分隔字符串 - How to validate comma separated string with space using Regex 从字符串语句中删除最后一个逗号 - remove last comma from string statement 如何使用正则表达式或C#从字符串中删除前6个和后6个字符 - How to remove First 6 and last 6 characters from a string using regex or c# 如何使用正则表达式从字符串中删除模式 - How to remove a pattern from a string using Regex 使用 regex.replace() 添加到 ", 和 " 到字符串中倒数第三个逗号 - Using regex.replace() to add to ", and "to the third to last comma in a string 仅当它是逗号时,如何从字符串中删除最后一个字符? - How do I remove the last character from a string only when it is a comma? 如何在逗号分隔的字符串中删除单词中的所有尾随空格(前后两个空格) - How do i remove all trailing space from word(both front and end white space) in comma seperated string 正则表达式以匹配字符串,空格或逗号的开头 - Regex to match start of string or space or comma 如何从字符串中删除逗号分隔值? - How to remove comma separated value from a string? 在 C# 中使用 Regex 获取最后一个逗号或最后一个数字之后的字符串 - Get string after the last comma or the last number using Regex in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM