使用 C# 删除分隔符
我有一个字符串如下:
{a{b,c}d}
如果我给1,字符串必须显示为:
{a d}
内部大括号内的内容应与大括号一起删除。
有人可以帮我吗?
I Have a string as follows:
{a{b,c}d}
if i give 1, the string must be displayed as:
{a d}
the content within inner braces should be removed along with the braces.
Can anyone help me with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要提取 {} 的内部分组,请使用以下正则表达式:
实际上,如果您想删除逗号....
要提取没有内部分组的外部分组:
如果在您的示例中,a、b、c、d 不是字面意思单个字符,即字母组甚至空格等,
根据您对嵌套的评论 将 \w 替换为 \w+ 甚至 .* ....
To extract the inner grouping of {} use the following regular expression:
Actually, if you want to remove the comma....
To extract the outer without the inner grouping:
if in your example a, b, c, d are not literally single characters, that is groups of letters or even spaces, etc replace \w with \w+ or even .*
based on your comment on nesting....
按照上面的正则表达式方式...它真的更漂亮!
你可以手工完成......几年前我在一个例子中写了一些括号......必须找一秒钟......:
写道:
go the regex-way above... it's really more beautiful!
you could do it by hand... i've written something for paranthesis in an example a few years ago... have to look for it a sec...:
writes:
命名空间分隔符
{
班级计划
{
静态无效主(字符串[]参数)
{
字符串 src = "a{b{c{d,e}f}g}h";
int 发生次数 = 0;
foreach(src 中的 char ch)
{
if(ch == '{')
{
发生次数++;
}
}
Console.WriteLine("请输入要删除的方块编号:");
int 给定值 = 0;
检查有效(出给定值);
}
namespace Delimiter
{
class Program
{
static void Main(string[] args)
{
string src = "a{b{c{d,e}f}g}h";
int occurenceCount = 0;
foreach (char ch in src)
{
if(ch == '{')
{
occurenceCount++;
}
}
Console.WriteLine("Enter a no. to remove block: ");
int givenValue = 0;
CheckValid(out givenValue);
}