"Find" function

Developer
Aug 3, 2010 at 2:29 AM

Hi Cathal,

   It would really be helpful to me to be able to invoke a method like:

List<string> Find("RegEx expression"); and get the list back of what it found.

Since Custom Properties are difficult to manipulate in a document, and are

essentially "hidden" to common users, it would let me find everything

like, for example, [Address] and then use the Replace function you already provide

to set it to a new value, which would obviously have no square brackets :)

I don't know what I'll find when I process a document, and prompt

my son for anything I can't find as a property name in the data object.

I hope this makes sense - we could really use the help!  Thanks - Brian

 

Coordinator
Aug 3, 2010 at 8:59 AM
Brian,

this is an excellent suggestion and I will try to find time soon to implement this for you. 
I have a few busy weeks ahead of me as I have just started my PhD but I will try to find the time.

Kind regards,
Cathal

Developer
Aug 14, 2010 at 2:50 AM

Cathal,

  To get the functionality I needed, I added the following toNovacode.Paragraph (painfully condensed for posting):

        public List<string> FindAllByPattern(string pattern, RegexOptions options)
        {    // find all unique instances of the given Regex Pattern
            MatchCollection mc = Regex.Matches(this.Text, pattern, options);

            var query =  (
                from m in mc.Cast<Match>()
                select m.Value
            ).ToList();
            return query;
        }

and added this to Novacode.Container (again, header excluded, etc.):

        public virtual List<string> FindAllByPattern(string pattern, RegexOptions options)
        {
            List<string> rawResults = new List<string>();

            foreach (Paragraph p in Paragraphs)
            {
                List<string> partials = p.FindAllByPattern(pattern, options);
                rawResults.AddRange(partials);
            }

            Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
            List<string> uniqueResults = new List<string>();

            foreach (string currValue in rawResults)
            {
                if (!uniqueStore.ContainsKey(currValue))
                {
                    uniqueStore.Add(currValue, 0);
                    uniqueResults.Add(currValue);
                }
            }

            return uniqueResults;
        }

I would appreciate any instructions for posting, contributing or otherwise ensuring that the functionality remains part of the code base so that I don't need to hack it back in each time :)  As always, thanks for all your help! - Brian

 

Coordinator
Aug 16, 2010 at 8:34 AM
Brian,

thank you for making a code contribution. At the moment I have a local
version of DocX that I am not ready to push to codeplex's source
control. I am afraid that until I push this version you cannot add to
the source tree. I will make sure that your code is added to my local
copy before I submit to codeplex.

Thanks again,
Cathal
Coordinator
Sep 2, 2010 at 9:35 AM
On Sat, Aug 14, 2010 at 3:50 AM, bdm <notifications@codeplex.com> wrote:

From: bdm

Cathal,

  To get the functionality I needed, I added the following toNovacode.Paragraph (painfully condensed for posting):

        public List<string> FindAllByPattern(string pattern, RegexOptions options)
        {    // find all unique instances of the given Regex Pattern
            MatchCollection mc = Regex.Matches(this.Text, pattern, options);

            var query =  (
                from m in mc.Cast<Match>()
                select m.Value
            ).ToList();
            return query;
        }

and added this to Novacode.Container (again, header excluded, etc.):

        public virtual List<string> FindAllByPattern(string pattern, RegexOptions options)
        {
            List<string> rawResults = new List<string>();

            foreach (Paragraph p in Paragraphs)
            {
                List<string> partials = p.FindAllByPattern(pattern, options);
                rawResults.AddRange(partials);
            }

            Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
            List<string> uniqueResults = new List<string>();

            foreach (string currValue in rawResults)
            {
                if (!uniqueStore.ContainsKey(currValue))
                {
                    uniqueStore.Add(currValue, 0);
                    uniqueResults.Add(currValue);
                }
            }

            return uniqueResults;
        }


Brian,

you mention that the above code is (painfully condensed for posting), by this do you mean

A) You shortened the code omitting anything that wasn't necessary to explain your point. 
B) This code is condensed because you spent ages working on it?

My question is can I add your code into DocX as is or do you have a non-condensed version that you would prefer I add?

Kind regards,
Cathal
Developer
Mar 10, 2011 at 12:51 AM

Just as a reminder,

   We've exchanged emails on this topic this week.  I would appreciate it if we could get this code added!  Thanks!