开发者

RegEx: Split String at Capitalized Letters and Non-capitalized letters to Create Small Cap Fonts

开发者 https://www.devze.com 2022-12-25 00:49 出处:网络
So i\'ve purposefully stayed away from RegEx as just looking at it kills me...ugh. But now I need it and could really use some help to do this in .NET (C# or VB.NET). I need to split a string based on

So i've purposefully stayed away from RegEx as just looking at it kills me...ugh. But now I need it and could really use some help to do this in .NET (C# or VB.NET). I need to split a string based on capitalization or lack thereof. For example:

I'm not up开发者_如何学GoPercase

  1. "I"
  2. "'m not up"
  3. "P"
  4. "ercase"

or

FBI Agent Winters

  1. "FBI A"
  2. "gent "
  3. "W"
  4. "inters"

The reason I'm doing this is to manually create small caps, in which non-capitalized strings will be sent to uppercase and their font size made 80% of the original font size. Appreciate any help that could be provided here.


Sounds to me like you just need to match anything that's not an uppercase letter. For example:

input = Regex.Replace(input, @"[^A-Z]+", ToSmallCaps);

...where ToSmallCaps is a MatchEvaluator delegate that converts the matched text to small caps, however it is you're doing that. For example:

static string ToSmallCaps(Match m)
{
  return String.Format(@"<span style=""whatever"">{0}</span>", m.Value);
}

EDIT: A more Unicode-friendly version regex would be @"[^\p{Lu}\p{Lt}]+", which matches one or more of anything other than an uppercase or titlecase letter, in any language.


Although Alan's answer will probably solve your problem, for completeness' sake, I'm posting a regex that returns both the uppercase and the lowercase parts as matches, like in your example.

ANSI:

Regex.Matches("I'm not upPercase", @"[^a-z]+|[^A-Z]+");

Unicode:

Regex.Matches("I'm not upPercase", @"[^\p{Ll}]+|[^\p{Lu}]+");


I think this regular expression should work /([A-Z ]*)([^A-Z]*)/

It makes those splits on that data according to rubular.com


I think this is also can be achieved using assertions in regular expressions:

<?php
$str = 'TestMyFuncCall';
var_dump(preg_split('/(?=[A-Z])/', $str, null, PREG_SPLIT_NO_EMPTY));

Output:

array(4) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(2) "My"
  [2]=>
  string(4) "Func"
  [3]=>
  string(4) "Call"
}

I'm sorry for PHP, no Visual Studio in the reach. But you can do pretty much the same in .NET I'm sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号