Solved

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

Posted on 2012-04-05
2
376 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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now