简体   繁体   English

如何用“ XX XXX”代替“ XX,XXX”?

[英]How can I replace “XX,XXX” with “XX XXX”?

I need to replace string like "XX,XXX" with "XX XXX". 我需要将“ XX,XXX”之类的字符串替换为“ XX XXX”。 The string "XX,XXX" is in another string, eg: 字符串“ XX,XXX”在另一个字符串中,例如:

"-1299-5,"XXX,XXXX",trft,4,0,10800"

The string is fetched from a text file. 该字符串是从文本文件中提取的。 I want to split the string by ",". 我想用“,”分割字符串。 But the comma in the substring led to the wrong result. 但是子字符串中的逗号导致了错误的结果。

The X represents a char. X代表一个字符。 I think regex can help, who can give me the right regex expression. 我认为正则表达式可以提供帮助,谁可以给我正确的正则表达式表达。

This expression, 这个表达,

(.*"[^,]*),([^,]*".*)

with a replacement of $1 $2 might work. 替换为$1 $2可能会起作用。

Demo 演示版

Example

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(.*""[^,]*),([^,]*"".*)";
        string substitution = @"\1 \2";
        string input = @"-1299-5,""XXX,XXXX"",trft,4,0,10800";
        RegexOptions options = RegexOptions.Multiline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
    }
}

Simply, use 'Replace' to replace char from your string. 只需使用“替换”来替换字符串中的char。

 var test = "XXX,XXXX";
 var filtered = test.Replace(',', ' ');  
 Console.WriteLine(filtered); 

Output : 输出:

XXX XXXX 

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

相关问题 如何选择xx-xxx-xx-x格式的ID? - How can i select some id with xx-xxx-xx-x format? 电话号码的正则表达式+7 - XXX - XXX - XX - XX - Regex for phone number +7 - XXX - XXX - XX - XX :无法连接到XX.XXX.XX.XXX:6379的Redis实例 - : could not connect to redis Instance at XX.XXX.XX.XXX:6379 AWS Cloud9无法连接到SSH服务器ubuntu@xx.xx.xxx.xx - AWS Cloud9 couldn't connect to SSH server ubuntu@xx.xx.xxx.xx EF codefirst ClientConnectionId:xx.xxx.xx.xx 错误号:4060,State:1,Class:11 - EF codefirst ClientConnectionId:xx.xxx.xx.xx Error Number:4060,State:1,Class:11 AADSTS50020 来自身份提供商“live.com”的用户帐户“xxx@zbc.com”在租户“xx-xx-xx-xx”中不存在 C# Winforms 错误 - AADSTS50020 User account 'xxx@zbc.com' from identity provider 'live.com' does not exist in tenant 'xx-xx-xx-xx' Error in C# Winforms 更新条目时发生错误。 xx.xxx超出范围 - An error occurred while updating the entries. xx.xxx Out of range 将社会安全号码 (SSN) 格式化为 XXXXXXXXX 中的 XXX-XX-XXXX - Format a Social Security Number (SSN) as XXX-XX-XXXX from XXXXXXXXX 我收到此错误 28000:在 .Net Core 项目中应用迁移时主机“XXX.xx.xx.192”没有 pg_hba.conf 条目 - iam getting this error 28000: no pg_hba.conf entry for host "XXX.xx.xx.192" while applying migrations in .Net Core Project 如何通过NetTopologySuite用xxx米制作缓冲区 - How can I make a buffer with xxx meter by NetTopologySuite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM