473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to get a part of a string using reg exp

I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". Is there a way?
--
Mike
Jun 14 '06 #1
8 1494
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 = "00sddsd00021dsd";

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

int intNum = int.Parse(theNum);

intNum++;

string finalString = s.Replace(theNum, intNum.ToString());
"Mike9900" <Mi******@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". 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******@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". Is there a way?
--
Mike

Jun 14 '06 #3
That turns the string from "00sddsd00021dsd" to "00sddsd22dsd". 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 = "00sddsd00021dsd";

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

int intNum = int.Parse(theNum);

intNum++;

string finalString = s.Replace(theNum, intNum.ToString());
"Mike9900" <Mi******@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". 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.com> wrote in message
news:eX**************@TK2MSFTNGP03.phx.gbl...
That turns the string from "00sddsd00021dsd" to "00sddsd22dsd". 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 = "00sddsd00021dsd";

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

int intNum = int.Parse(theNum);

intNum++;

string finalString = s.Replace(theNum, intNum.ToString());
"Mike9900" <Mi******@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". 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
"00vv00fg00021dsd", and so we want to produce "00vv00fg00022dsd.
--
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 = "00sddsd00021dsd";

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

int intNum = int.Parse(theNum);

intNum++;

string finalString = s.Replace(theNum, intNum.ToString());
"Mike9900" <Mi******@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I need to get 00021 in "00sddsd00021dsd" and then increment it to become
"00sddsd00022dsd". 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
"00vv00fg00021dsd", and so we want to produce "00vv00fg00022dsd.

Jun 14 '06 #7
On Wed, 14 Jun 2006 13:22:02 -0700, Mike9900
<Mi******@discussions.microsoft.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
"00vv00fg00021dsd", and so we want to produce "00vv00fg00022dsd.


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 = "00vd00asd00999asdkk";

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

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

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

Console.WriteLine(result);

--
Marcus Andrén
Jun 15 '06 #8
Marcus Andrén wrote:
On Wed, 14 Jun 2006 13:22:02 -0700, Mike9900
<Mi******@discussions.microsoft.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
"00vv00fg00021dsd", and so we want to produce "00vv00fg00022dsd.


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 = "00vd00asd00999asdkk";

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

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

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

Console.WriteLine(result);


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

Regex rx = new Regex(@"\d+");
string s = "00vv00fg00021dsd";
s = rx.Replace(s, new MatchEvaluator(this.Increment));
public string Increment(Match m)
{
int i = int.Parse(m.Value);
i++;
return i.ToString().PadLeft(m.Value.Length, '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.Value);
if (i > 0) { i++; }
return i.ToString().PadLeft(m.Value.Length, '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
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...
5
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
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...
4
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...
4
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,...
2
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...
0
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...
3
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 ...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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,...
0
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...
0
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,...
0
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...

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.