i want to create a regular expression to find and replace uppercase character based on some condition.
Find the starting uppercase for a group of uppercase character in a string and replace it lowercase and "*" before the starting uppercase.
If there is any lowercase following the uppercase,replace the uppercase with lowercase and "*" before the starting uppercase.
input string : stackOVERFlow
expected output : stack*over*flow
i tried but could not get it working perfectly.
Any idea on how to create a regular expression ?
Thanks
kalkal
Member
18 Points
11 Posts
how to create regular expression based on some condition
May 02, 2012 08:36 PM|LINK
Hi,
i want to create a regular expression to find and replace uppercase character based on some condition.
Find the starting uppercase for a group of uppercase character in a string and replace it lowercase and "*" before the starting uppercase.
If there is any lowercase following the uppercase,replace the uppercase with lowercase and "*" before the starting uppercase.
input string : stackOVERFlow
expected output : stack*over*flow
i tried but could not get it working perfectly.
Any idea on how to create a regular expression ?
Thanks
Paul Linton
Star
13431 Points
2535 Posts
Re: how to create regular expression based on some condition
May 02, 2012 11:41 PM|LINK
I don't do regular expressions but Linq is quite up to the challange
string x = "stackOVERFlow"; var y = x.ToCharArray() .Skip(1) .Select((c,pos) => ((char.IsLower(x[pos]) && char.IsUpper(x[pos+1])) || //lower to upper (char.IsUpper(x[pos+1]) && x.Length>pos+1 && char.IsLower(x[pos+2])) // upper to lower ? "*" : string.Empty ) + char.ToLower(x[pos+1])) .Aggregate (x.Substring(0,1), (sofar,c) => sofar+c );