Use Regex in C # to insert spaces between ConcatenatedFileNamesLikeThis?

We are trying to create user-friendly report names from SSRS file names, they cannot have spaces in the name, but they can be generated from report names using a regular expression to scan caps / nocaps and alpha / numerical borders. However, all caps complicate the situation. For instance:

ListOfMembers => List Of Members Weekly123Report => Weekly 123 Report MembersOfIEETeam => Members Of IEE Team 

So, I think this is a minimal set of rules;

 (a-z0-9)(AZ) gets replaced with "$1 $2" (AZ)?(AZ)(az) gets replaced with "$1$2 $3" (A-Za-z)(0-9) gets replaced with "$1 $2" (0-9)(A-Za-z) gets replaced with "$1 $2" 

Can this be done in one fell swoop or will it take several passes? Suppose we had a report file name something like this:

 WeeklyIEEReportWC20090103SortedByDate 

I saw that SSRS does something similar when it comes to series names on diagrams, and generates them on the fly from the concatenated version.

Any information appreciated! :)

+4
source share
2 answers

My interpretation and solution:

 var input = "WeeklyIEEReportWC20090103SortedByDateXFoo3W3CBar4x"; var re = @"(?!^)(?:[AZ](?:[az]+|(?:[AZ\d](?![az]))*)|\d+)"; string value = Regex.Replace(input, re, " $0"); 

Result: Weekly IEE Report WC20090103 Sorted By Date X Foo 3 W3C Bar 4x

+5
source

Change 2 fixed IEE

 var input = @"WeeklyIEEReportWC20090103SortedByDate"; string p = @"(?<=[AZ])(?=[AZ][az])|(?<=[a-z0-9])(?=[AZ])|(?<=[a-zA-Z])(?=[0-9])"; string value = Regex.Replace(input, p, " "); 

produces Members Of IEE Team and Weekly IEE Report WC 20090103 Sorted By Date for provided samples.

+1
source

All Articles