拆分PascalCase字符串(首字母缩写词除外) [英] Split PascalCase string except for acronyms

查看:169
本文介绍了拆分PascalCase字符串(首字母缩写词除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词列表,需要使它们易于阅读,例如FirstName到First Name,LastName到Last Name,在某些情况下,首字母缩写词(如ARB)保持原样.由于正则表达式返回 AR Bs ,最近引入了后者,并引起了显示问题.这就是我们所拥有的,我知道这对于首字母缩写词是不够的:

I have a list of words that need to be made human readable, such as FirstName to First Name, LastName to Last Name, and in some cases, acronyms like ARBs to remain as is. The latter was recently introduced and has caused a display issue since our regular expression returns AR Bs. Here's what we have, which I know is insufficient for acronyms:

([A-Z][a-z]+)

我在SO和其他站点上找到了能够使用首字母缩写词的其他表达式,但是它们可用于首字母缩写词位于字符串内而不是整个字符串的字符串.我可以做简单的正则表达式,但这对我的技能来说太棘手了.我将提供其他示例来测试是否有它们,但是除了新的ARB外,所有字符串都可以正常工作.谢谢.

I've found other expressions on SO and on other sites that are able to work with acronyms, however they work on strings where the acronym is within the string rather than being the entire string. I can do simple regular expressions, but this is too tricky for my skills. I would provide other examples for testing if I had them, but all of the strings work fine except the new one, ARBs. Thank you.

更新:这是代码用法

string friendlyName = Regex.Replace(field.Name, "([A-Z][a-z]+)", " $1", RegexOptions.Compiled).Trim();

推荐答案

[A-Z]+[a-z]*不会吗?应该匹配一个或多个大写字母,然后匹配零个或多个小写字母.因此ARBs将保持为单个实体,但CamelCase将被拆分为Camel Case.

Wouldn't [A-Z]+[a-z]* do it? That should match one or more upper-case letters followed by zero or more lower-case letters. So ARBs would remain a single entity, but CamelCase would be split into Camel Case.

这篇关于拆分PascalCase字符串(首字母缩写词除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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