0

let's say I have a source file that matches a certain filepattern, and I want certain elements of the file pattern to be reused in a newly created destination file.
so for ex. the specified file pattern given in regex is

src_pattern = "(\d\d)_(\d\d)_(\d\d\d\d)_FOO.xml";

after searching a directory, found a matching source file looking like 

src = 08_21_2013_foo.xml

now the new file must be of the form 

dst = $3$1$2_BAR.xml;  

where the capturing groups pulled from the source (so would look like 20130821_BAR.xml). How would I go about accomplishing this efficienly, needs to pretty flexible and I have no knowledge of what each of these look like, they are being pulled from somewhere else. so I guess I'm having trouble with pulling the numberings for the capturing groups, ie the 3rd, the 1st, then the 2nd and let's say I found it, how do I reference it back to the source file. would I have to have an integer (say k) that represents the number and reference it like

match = Regex.Match(src, src_pattern)
match.Groups[k].Value

pulling these numberings seem to be a pain...

I also have no idea how many of these capturing groups each dst specifies so how to automate all of this? Is there another way or some smart native functonality for this

0

3 Answers 3

1

Try with

var rx = new Regex(@"^(\d\d)_(\d\d)_(\d\d\d\d)(?=_FOO\.xml$)", RegexOptions.IgnoreCase);
var res = rx.Replace("08_21_2013_foo.xml", "$3$1$2");

Note the RegexOptions.IgnoreCase, the use of the ^ and $ to force the regex to consider the whole string and the (?=_FOO.xml$) that means "followed by _FOO(end of the string)", but that isn't a capturing group.

Sign up to request clarification or add additional context in comments.

1 Comment

You forgot _BAR at the end of the file name.
1

If you could get them to use named groups (http://msdn.microsoft.com/en-us/library/bs2twtah.aspx#named_matched_subexpression), you could easily just run a substitute based on whatever they requested and have them name them again in the resulting output dst. For example:

src_pattern = "(<first>\d\d)_(<second>\d\d)_(<third>\d\d\d\d)_FOO.xml";

after searching a directory, found a matching source file looking like 

src = 08_21_2013_foo.xml

now the new file must be of the form 

dst = "[third][first][second]_BAR.xml";  

3 Comments

it's all specified and numbered groups instead of named groups. the most important part is I do not know beforehand I want the third, the first, then the second so can't hardcode these.
You need to expand upon your question then... if you don't know beforehand, at what point will you know? From your example above it appeared these settings are config file driven.
I said it's coming from a different source, a data file or sql perhaps but there are thousands if not more, I don't know what the src and dst look, that was just one specific example.
1
  • So we take the regex and provide some sample data.
  • for each match in mockMatches which you would replace with the enumerator that pulls in your list of filenames.
  • we then match the regex using the src_pattern with ignore case turned on.
  • then we want the matching groups which comes out as a GroupCollection so we Cast<T>() that Enumerable as IEnumerable<Group>
  • skip the first group (which is the entire match) using Skip(1)
  • then get the group's value (the actual text of the group match) with .Select(a=>a.Value)
  • join all of those together using a _ as the seperator with .Aggregate((s1,s2)=>s1+"_"+s2)
  • then add the file ending constant "_bar.xml"

Linqpad based answer:

var src_pattern= @"(\d\d)_(\d\d)_(\d\d\d\d)_FOO\.xml";
var mockMatches = new[]{"08_21_2013_foo.xml"};
foreach(var mm in mockMatches){
  var match = Regex.Match(mm,src_pattern, RegexOptions.IgnoreCase).Dump();
  var dst= match.Groups.Cast<Group>().Skip(1).Select(a=>a.Value).Aggregate((s1,s2)=>s1+"_"+s2)+"_bar.xml";
  dst.Dump();
}

4 Comments

what is cast and dump, sorry never used linqpad
Cast<T> is a Linq method, Dump is kinda like console.writeline but WAY better.
can you roughly explain the logic
i really appreciate great solution but I have a more general question in a larger context really

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.