Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
reading excel file in .net 2 46
Shared Service Environment 2 55
input validations, TryUpdateModel and model binding  in asp.net mvc 5 25
dropdownlist in asp.net vb. 3 24
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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