Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Help with Regex Pattern

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Naveen - 16 Sep 2007 18:08 GMT
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.
Naveen - 16 Sep 2007 18:32 GMT
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.

> Not sure if this is the right forum for this question but couldn'd find
> another newsgroup.
[quoted text clipped - 13 lines]
>
> Thanks.
Jesse Houwing - 16 Sep 2007 22:29 GMT
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 :
>
[quoted text clipped - 22 lines]
>>
>> Thanks.

--
Jesse Houwing
jesse.houwing at sogeti.n
Arne Vajhøj - 16 Sep 2007 19:41 GMT
> Not sure if this is the right forum for this question but couldn'd find
> another newsgroup.
[quoted text clipped - 11 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.