简体   繁体   中英

split camelCase str regex if characters is upper case?

I have this random test random string "... Jc Yikr Cuduxlor B Az I Ngt FT Jtcmvs Hnrzsv Saegaowyta I..." to test regex "@"(\\P{Ll})(\\P{Ll}\\p{Ll})", "$1 $2" ), @"(\\p{Ll})(\\P{Ll})", "$1 $2" " to split camelCase. the problem came FT the regex make "FT" how to make the regex ignore the two string are uppercase?

Expected string length 186 but was 185. Strings differ at index 82.
  Expected: "... Jc Yikr Cuduxlor B Az I Ngt F T Jtcmvs Hnrzsv Saegaowyta ..."
  But was:  "... Jc Yikr Cuduxlor B Az I Ngt FT Jtcmvs Hnrzsv Saegaowyta I..."
  --------------------------------------------^

  public static string BreakCamelCase(string str)
  {
   return Regex.Replace( Regex.Replace( str, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2" ), @"(\p{Ll})(\P{Ll})", "$1 $2" );
  }

How about using a lookahead?

Just to check whether an uppercase character is following it.
But without actually consuming that uppercase character.

And that way the replace string only needs 1 capture group.

RegEx Pattern

(\p{L})(?=\p{Lu})

C#

public static string BreakCamelCase(string str)
{
   return Regex.Replace(str, @"(\p{L})(?=\p{Lu})", "$1 ");
}

Explaining the pattern

(\\p{L}) : 1st Capturing Group ($1) matching any kind of letter from any language

(?=\\p{Lu}) : Positive Lookahead to check for an uppercase letter that has a lowercase variant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM