473,795 Members | 2,875 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 "FirstnameLastn ame" 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 4875
By the way, the closest I got was :

string testString = "FirtsnameLastn ame";
string[] s = Regex.Split(tes tString, @"[^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 "FirstnameLastn ame" 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 "FirstnameLastn ame" 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.Reg ularExpressions ;

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

Arne
Sep 16 '07 #3
Hello Naveen,

string testString = "FirstnameLastn ame";
if (teststring.ind exOf(" ") == -2)
{
string testString = Regex.Replace(t eststring, @"([a-z])([A-Z])", @"$1 $2",
RegexOptions.No ne);
}

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(tes tString, "\s", RegexOptions.No ne).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 insertWhitespac eRegex = new Regex(@"([a-z])([A-Z])",
RegexOptions.Co mpiled);
public static string InsertWhitespac e(string testString)
{
if (teststring.ind exOf(" ") == -2)
{
string testString = insertWhitespac eRegex.Replace( teststring, @"$1 $2");
}
}
Jesse
By the way, the closest I got was :

string testString = "FirtsnameLastn ame";
string[] s = Regex.Split(tes tString, @"[^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 "FirstnameLastn ame" 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
9772
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 numeric value according to an arbitrary regular expression.
2
1650
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 trying to use Regex in C#. Basically, I have a fixed format text file which I need to validate prior to using in a program. The validation insures the data format matches what the program is expecting to find in the file. The pattern I am trying to...
16
2166
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 RegEx.IsMatch but it behaves quite differently. Is there a way I can mimic the DOS filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls" returns all excel files, "workbook*" returns all files begining with "workbook" etc)? thanks in...
5
11347
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 variable on which I need to perform log and then divide the result with 3.980. While parsing such expressions, I need to find all occurence of date values and replace "/" character with "~" character. I need to do this only for date portion of the
9
4259
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). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually hardcoded the "\t") Private Shared Function replaceAll(ByVal strIn As String, ByVal strFind As...
1
3724
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
8
10289
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 bunch of hits with this - I'm only interested in the *last* one. Is there a way to build the concept "last" into the
5
1308
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 Modules. but for some reason everytime i try to call the function i get an error of "Name 'GeneralMethods' is not defined". here is my module, as you can see very simple
4
1265
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: 99,"01/01/2007","23,000",1,34,"henry",132,"45.00" I used some code from an article that I though would do what I needed, but it splits my amount fields(76,000 into two different fields : 76, and 000... Do I change my pattern ? I never used regex before and need some help. thank you.
10
1575
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 regex and the .groups method. Please help. d|d|d|string of text 1||s|s|||dd.dd|ss|string of text 2||||||||||||||||||||||||||string of text 2 I only want string of text1
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.