正则表达式的字符串(取出一个字符,并用空格代替吧) [英] Regex string (taking out a character and replacing it with a space)

查看:159
本文介绍了正则表达式的字符串(取出一个字符,并用空格代替吧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的在一个辅助扩展

I have the following in a helper extension:

return Regex.Replace(str,
"(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)",
 " $1");

这将有效采取

WhateverIPutInToIt

WhateverIPutInToIt

和渲染

无论我把它到

。但是,如果我不那么利用它不会与空间渲染。

except that I would prefer not to capitalize each proper word. However, if I do not capitalize then it will not render with the spaces.

我想我会,而不是尝试采取类似

I thought I would instead try to take something like

Whatever_I_put_into_it

Whatever_I_put_into_it

渲染我想要什么。我无法弄清楚如何做一个正规的前pression(如果它甚至有可能)剥离出的下划线字并用空格代替它。

to render what I want. I cannot figure out how to do a regular expression (if its even possible) to strip out the the "underscore" character and replace it with a space.

任何帮助吗?

更新:
对不起,我没有说清楚。这是一个辅助扩展枚举我有转换成可读的格式。我使用我的dropdownlists和checkboxlists的帮手。我有超过10枚举那些,和单独做每个人都成为一个痛苦。希望我可以只修复 Regex.Replace code。在顶部的东西,将实现我追求的。

Update: Sorry for not being clear. This is in a helper extension to convert enums I have into something readable. I am using the helper for my dropdownlists and checkboxlists. I have over 10 enums for those, and to do each one individually would be a pain. Was hoping that I could just fix the Regex.Replace code at the top to something that would accomplish what I am after.

你要知道,如果我做

YouHave9OrMore

YouHave9OrMore

以上 Regex.Replace 会吐出(它打出的数字9)

the above Regex.Replace will spit out (it splits out the number "9")

您有9个或更多

感谢大家的快速意见。

推荐答案

您是否尝试过您正则表达式与MatchEvaluator参数?

Have you tried your regex with MatchEvaluator parameter?

var finalstr = Regex.Replace(
  "WhateverIPutInToIt", 
  "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)", 
  me => " " + me.Value.ToLower() // <----
);

这将输出

This would output Whatever i put in to it.

YouHave9OrMore ==> 你有9个或更多

YouHave9OrMore ==> You have 9 or more

如果你把它的扩展方法,你可以使用它像这样

If you make it an extension method you can use it like this

var finalstr = System.IO.SearchOption.TopDirectoryOnly.GetString();

public static class MyExtensions
{
    public static string GetString(this Enum en)
    {
        return Regex.Replace(en.ToString(), "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)", me => " " + me.Value.ToLower());
    }
}

这篇关于正则表达式的字符串(取出一个字符,并用空格代替吧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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