使用正则表达式仅用内引号替换空格 [英] Replace space with only inside quotes using Regex

查看:44
本文介绍了使用正则表达式仅用内引号替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

" smth, value, 'val ', ' name surname' "

我想用 ¤ 替换引号内的空格,所以结果是:

And I want to replace the white space inside quotes with ¤ so the result will be:

" smth, value, 'val¤', '¤name¤surname' "

请问,有什么办法可以用正则表达式来做吗?

Please, is there any way to do it with regex?

推荐答案

您可以使用正则表达式查找配额中的文本.然后使用简单的字符串替换

You can use regex to find text in quotas. Then use simple string replace

var input = " smth, value, 'val ', ' name surname' ";
var result =  Regex.Replace(input, "'(?<text>[^']+?)'", m =>
{
    var replacement = "¤";
    return $"'{m.Groups["text"].Value.Replace(" ", replacement)}'";
});

这篇关于使用正则表达式仅用内引号替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆