普通EX pression,分割字符串大写字母,但忽略了TLA [英] Regular expression, split string by capital letter but ignore TLA

查看:180
本文介绍了普通EX pression,分割字符串大写字母,但忽略了TLA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用正则表达式

  System.Text.RegularEx pressions.Regex.Replace(stringToSplit,([AZ]),$ 1)。修剪()
 

由大写字母来分割字符串,例如:

MyNameIsSimon变成我的名字叫西蒙

我用枚举工作时,发现这非常有用。我想这样做的是稍微改变它,以便串是只有分裂当的下一步的字母是小写字母,例如:

今日美国将成为今日美国

可以这样做?

编辑:感谢所有的响应。也许我还没有完全想这个通过,在某些情况下,'A'和'我'将需要被忽视,但是这是不可能的(至少不是以有意义的方式)。在我的情况下,虽然答案下面做什么,我需要。谢谢!

解决方案

(?(小于?= [A-Z])[A-Z] | [A-Z](= [A-Z]))

或它的Uni code-知道表弟

((小于?= \ p {LL})\ p {路} | \ p {路}(= \ p {LL}))

在与全球范围内更换

 $ 1
 

手柄

TodayILiveInTheUSAWithSimon
今日美国
IAmSOOOBored

收益率

 今天,我住在美国与西蒙
今日美国
我是SOOO无聊

在第二个步骤中,您将不得不削减字符串。

I'm using the regex

System.Text.RegularExpressions.Regex.Replace(stringToSplit, "([A-Z])", " $1").Trim()

to split strings by capital letter, for example:

'MyNameIsSimon' becomes 'My Name Is Simon'

I find this incredibly useful when working with enumerations. What I would like to do is change it slightly so that strings are only split if the next letter is a lowercase letter, for example:

'USAToday' would become 'USA Today'

Can this be done?

EDIT: Thanks to all for responding. I may not have entirely thought this through, in some cases 'A' and 'I' would need to be ignored but this is not possible (at least not in a meaningful way). In my case though the answers below do what I need. Thanks!

解决方案

((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))

or its Unicode-aware cousin

((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))

when replaced globally with

" $1"

handles

TodayILiveInTheUSAWithSimon
USAToday
IAmSOOOBored

yielding

 Today I Live In The USA With Simon
USA Today
I Am SOOO Bored

In a second step you'd have to trim the string.

这篇关于普通EX pression,分割字符串大写字母,但忽略了TLA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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