473,806 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get a part of a string using reg exp

I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike
Jun 14 '06 #1
8 1522
Yep.

Get the substring, parse it to an int, increment it, then turn it back into
a string and put it back in.

Havent compiled this but this should work:

string s = "00sddsd00021ds d";

string theNum = s.Substring(7, 5);

int intNum = int.Parse(theNu m);

intNum++;

string finalString = s.Replace(theNu m, intNum.ToString ());
"Mike9900" <Mi******@discu ssions.microsof t.com> wrote in message
news:C4******** *************** ***********@mic rosoft.com...
I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike

Jun 14 '06 #2
Let me know if it does work by the way Mike, did it in about 1 min so
fingers crossed.
"Mike9900" <Mi******@discu ssions.microsof t.com> wrote in message
news:C4******** *************** ***********@mic rosoft.com...
I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike

Jun 14 '06 #3
That turns the string from "00sddsd00021ds d" to "00sddsd22d sd". You have
to specify the format when you turn the incremented value back into a
string.

Daniel wrote:
Yep.

Get the substring, parse it to an int, increment it, then turn it back into
a string and put it back in.

Havent compiled this but this should work:

string s = "00sddsd00021ds d";

string theNum = s.Substring(7, 5);

int intNum = int.Parse(theNu m);

intNum++;

string finalString = s.Replace(theNu m, intNum.ToString ());
"Mike9900" <Mi******@discu ssions.microsof t.com> wrote in message
news:C4******** *************** ***********@mic rosoft.com...
I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike


Jun 14 '06 #4
lol oops, as i say , did make the code in 1min and yep you would......geez
give me a break lol

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:eX******** ******@TK2MSFTN GP03.phx.gbl...
That turns the string from "00sddsd00021ds d" to "00sddsd22d sd". You have
to specify the format when you turn the incremented value back into a
string.

Daniel wrote:
Yep.

Get the substring, parse it to an int, increment it, then turn it back
into a string and put it back in.

Havent compiled this but this should work:

string s = "00sddsd00021ds d";

string theNum = s.Substring(7, 5);

int intNum = int.Parse(theNu m);

intNum++;

string finalString = s.Replace(theNu m, intNum.ToString ());
"Mike9900" <Mi******@discu ssions.microsof t.com> wrote in message
news:C4******** *************** ***********@mic rosoft.com...
I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike


Jun 14 '06 #5
It sounds nice, thanks.

But we do not know the length of the string, nor we know the format. We
only want to increment the number embeded inside the string. The string
could be like
"00vv00fg00021d sd", and so we want to produce "00vv00fg00022d sd.
--
Mike
"Daniel" wrote:
Yep.

Get the substring, parse it to an int, increment it, then turn it back into
a string and put it back in.

Havent compiled this but this should work:

string s = "00sddsd00021ds d";

string theNum = s.Substring(7, 5);

int intNum = int.Parse(theNu m);

intNum++;

string finalString = s.Replace(theNu m, intNum.ToString ());
"Mike9900" <Mi******@discu ssions.microsof t.com> wrote in message
news:C4******** *************** ***********@mic rosoft.com...
I need to get 00021 in "00sddsd00021ds d" and then increment it to become
"00sddsd00022ds d". Is there a way?
--
Mike


Jun 14 '06 #6
That string contains three numbers. How do you know which one should be
incremented?

Mike9900 wrote:
It sounds nice, thanks.

But we do not know the length of the string, nor we know the format. We
only want to increment the number embeded inside the string. The string
could be like
"00vv00fg00021d sd", and so we want to produce "00vv00fg00022d sd.

Jun 14 '06 #7
On Wed, 14 Jun 2006 13:22:02 -0700, Mike9900
<Mi******@discu ssions.microsof t.com> wrote:
It sounds nice, thanks.

But we do not know the length of the string, nor we know the format. We
only want to increment the number embeded inside the string. The string
could be like
"00vv00fg00021 dsd", and so we want to produce "00vv00fg00022d sd.


You have been a little vague, so I can only give an on what you have
given.

Assuming it is is a five digit number and it is the only five digit
number in the string, this should work.

string input = "00vd00asd00999 asdkk";

Match m = Regex.Match(inp ut,@"(.*)(\d{5} )(.*)");

int val = Convert.ToInt32 (m.Groups[2].Value);
val++;

string result = m.Groups[1].Value +
val.ToString("0 0000") +
m.Groups[3].Value;

Console.WriteLi ne(result);

--
Marcus Andrén
Jun 15 '06 #8
Marcus Andrén wrote:
On Wed, 14 Jun 2006 13:22:02 -0700, Mike9900
<Mi******@discu ssions.microsof t.com> wrote:
It sounds nice, thanks.

But we do not know the length of the string, nor we know the format. We
only want to increment the number embeded inside the string. The string
could be like
"00vv00fg00021d sd", and so we want to produce "00vv00fg00022d sd.


You have been a little vague, so I can only give an on what you have
given.

Assuming it is is a five digit number and it is the only five digit
number in the string, this should work.

string input = "00vd00asd00999 asdkk";

Match m = Regex.Match(inp ut,@"(.*)(\d{5} )(.*)");

int val = Convert.ToInt32 (m.Groups[2].Value);
val++;

string result = m.Groups[1].Value +
val.ToString("0 0000") +
m.Groups[3].Value;

Console.WriteLi ne(result);


You can rewrite this using a MatchEvaluator to make it a little easier
(and faster):

Regex rx = new Regex(@"\d+");
string s = "00vv00fg00021d sd";
s = rx.Replace(s, new MatchEvaluator( this.Increment) );
public string Increment(Match m)
{
int i = int.Parse(m.Val ue);
i++;
return i.ToString().Pa dLeft(m.Value.L ength, '0') ;
}

This also increments the other two 00 numbers though, but it wouldn't be
too hard to exclude those. Using the following MatchEvaluator instead
would skip any number that is equal to 0.

public string Increment(Match m)
{
int i = int.Parse(m.Val ue);
if (i > 0) { i++; }
return i.ToString().Pa dLeft(m.Value.L ength, '0') ;
}

Jesse Houwing
Jun 15 '06 #9

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

Similar topics

1
53511
by: Matthias Ludwig | last post by:
I'm trying to create a directory on the web server with a vb.net code: .... Dim dirName As String = "w:\filepath\images" If Not Directory.Exists(dirName) Then Directory.CreateDirectory(dirName) Label1.Text = "Directory created" Else
5
1567
by: ad | last post by:
I have a string like string myString="dog,cat,dog,tiger" I want to delete the duplication part, and make myString to "dog,cat,tiger" How can I do that?
1
5316
by: SMG | last post by:
Hi All, My Send mail system was working very fine, today suddenly it started giving me following error. Can any one suggest why this is happening so. I am using system.Web.Mail "The requested body part was not found in this message. " Note : here XXXX and YYYYYYY are company specific info and nothing related with code.
4
2815
by: Jason | last post by:
I need to open a file for reading, but I only know part of it's name. The file I want to read is in the format of xxx-yyyy-zzz.EXT The last three digits of the file name are different on each pc, for example PC7 has xxx-yyyy-zz1.ext PC5 has xxx-yyyy-zz2.ext PC3 has xxx-yyyy-zz8.ext
4
2070
by: SteveW | last post by:
Thanks to some good help from a previous post, I have been able to create well formed xml as part of a report logger app. However, I still have a small problem. When I add new xml to the log file, the new nodes are appended to the original file. What I really want is for the log file to grow as "report nodes" are added. Also, I am a bit concerned about performance, particularly as the file grows in size. I must write to an xml file (vs. a...
2
2583
by: Ken Crismon | last post by:
Hello, I am currently working on an embedded systems project where by I have written a small web server that is being hosted on our internet appliance (running on an Atmega128 chip and doing lots of col RFID and Mesh network stuff). In this example I am trying to get the AuthenticateUser web service to work. I am using the following WSDL:
0
7118
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
3
1557
by: wassimdaccache | last post by:
I'm using this code to search a value on a continuous form it is working properly Am I able to write for example part of the string Dim strCriteria As String Dim rst As DAO.Recordset Set rst = Me.RecordsetClone strCriteria = " = " & Me! rst.FindFirst strCriteria If rst.NoMatch Then
1
27121
Curtis Rutland
by: Curtis Rutland | last post by:
How To Use A Database In Your Program Part II This article is intended to extend Frinny’s excellent article: How to Use a Database in Your Program. Frinny’s article defines the basic concepts of using databases very well and is prerequisite reading for this article. Frinny’s article explains how to use a SQL Server in your program, but there are other databases as well. Some of them provide .NET connectors, but for those that don’t,...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
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
10366
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...
0
9187
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
7649
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
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 we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.