Hello Naveen,
string testString = "FirstnameLastname";
if (teststring.indexOf(" ") == -2)
{
string testString = Regex.Replace(teststring, @"([a-z])([A-Z])", @"$1 $2",
RegexOptions.None);
}
should do the trick. it finds the location of a small character, followed
by a capital character anf inserts a space by splitting them up.
The asiest way to ignore the result if there is a whitespace in teh string
is either checking for a specific space character. A more complicated test
would be:
Regex.Match(testString, "\s", RegexOptions.None).Success
This should test for all possible space characters in the unicode character
set.
If you're using this regex in a time critical way, or use it pretty often
is is better to use an instance of the regex instead of static calls.
private static Regex insertWhitespaceRegex = new Regex(@"([a-z])([A-Z])",
RegexOptions.Compiled);
public static string InsertWhitespace(string testString)
{
if (teststring.indexOf(" ") == -2)
{
string testString = insertWhitespaceRegex.Replace(teststring, @"$1 $2");
}
}
Jesse
By the way, the closest I got was :
string testString = "FirtsnameLastname";
string[] s = Regex.Split(testString, @"[^A-Z]?[a-z]*");
Unfortunately this returns F and L wheras I would like Firstname and
Lastname to be returned.
"Naveen" wrote:
>Not sure if this is the right forum for this question but couldn'd
find another newsgroup.
I am new to Regular expressions and would like help in deciding which
pattern allows me to split a string into sets of words based on
capital
letter.
For e.g. if i have a string "FirstnameLastname" I would like the
result to
return me Firstname and Lastname.
The other conditions of the input string are
1) If whitespace exists, do not return a match, instead return the
original
string.
2) The first character will always be a capital letter.
3) There may or may not be more than one word in the input string.
Any help will be greatly appreciated.
Thanks.
--
Jesse Houwing
jesse.houwing at sogeti.nl