Help with Regex Pattern

G

Guest

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.
 
G

Guest

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.
 
G

Guest

Naveen said:
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.

For inspiration:

using System;
using System.Text.RegularExpressions;

namespace E
{
public class PascalParse
{
private static Regex pascal = new Regex("[A-Z][a-z]*");
public static void Main(string[] args)
{
string s = "FirtsnameLastname";
foreach(Match m in pascal.Matches(s))
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
}
}

Arne
 
J

Jesse Houwing

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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top