473,503 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Regex Pattern

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.
Sep 16 '07 #1
3 4859
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.
Sep 16 '07 #2
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.
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
Sep 16 '07 #3
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
Sep 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9700
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
1642
by: Mortimer Schnurd | last post by:
Hi All, I am a VB 6 programmer who is now trying to learn C#. In doing so, I am trying to convert some of my VB modules to C#. I routinely user Reg Expressions in VB and am having some trouble...
16
2129
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
5
11337
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
9
4236
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
1
3694
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
8
10262
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
5
1290
by: Nemisis | last post by:
Hi everyone, I am currently building my .Net website, everything was fine, until i decided to put some generic functions into a module. The module is located in App_Code, with 2 other of my...
4
1254
by: =?Utf-8?B?bWFnZ2ll?= | last post by:
hi, I need some help with a reg. expression. I have a comma delimited file with quotes. Not every field has quotes, only some. This is a sample of my file:...
10
1551
by: supercrossking | last post by:
I am trying to the values of string of text in the sample before. The ds are for digits and s is for string and string of text is for a string with more than one or two values. I am trying to use...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.