Solved

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

Posted on 2012-04-05
2
389 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 500 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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