Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#.net subtract value from string using a regular expression

Posted on 2012-04-05
2
Medium Priority
?
391 Views
Last Modified: 2012-04-05
I'm using the regular expression shown below to find a given string in a text file.  That parts works.  The problem is that I need to subtract 7 from the number in that string in the file.  My code below is currently just writing SE*[d-[7]]*0001~ to the text file instead of updating it.  That makes sense since I put that in the replace portion of the command, but I don't know how to make this work otherwise.  Any ideas?

File.WriteAllText(
                  file,
                  Regex.Replace(File.ReadAllText(file), @"SE\*\d+\*0001~", "SE*[d-[7]]*0001~", RegexOptions.Multiline)
                );

Open in new window


Here is an example of the string I need to subtract 7 from:

SE*2078*0001~

I need it to say:

 SE*2071*0001~
0
Comment
Question by:fcsIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37812314
Please try this:
File.WriteAllText(
  file,
  Regex.Replace(File.ReadAllText(file), @"SE\*\d+\*0001~", new MatchEvaluator(this.Subtract), RegexOptions.Multiline)
);

// Add this method to your class
private string Subtract(Match m)
{
	var split = m.Value.Split('*');
	
	split[1] = (int.Parse(split[1]) - 7).ToString();
	
	return split[0] + "*" + split[1] + "*" + split[2];
}

Open in new window

0
 

Author Comment

by:fcsIT
ID: 37812396
I just realized I actually don't have to do this logic.  I will accept your answer as the right one though, you earned it on yesterday's problem!  :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question