none
using range.find.execute wildcard with special characters c# RRS feed

  • Question

  • Hello,

    this is probably a stupid question, but I was unable to find a solution searching the forums or the web.

    I am using the following code snipped to extract some tagged text from copy/pasted content in word:

    public void myPaste(Office.IRibbonControl control, bool cancelDefault)
            {
                cancelDefault = true;
                Word.Range rng = Globals.ThisAddIn.Application.Selection.Range;
                rng.Paste();
                rng.Find.ClearFormatting();
                object findText = "DDNA*/DDNA";
                object oTrue = true;
                object oFalse = false;
                object oFindStop = Word.WdFindWrap.wdFindStop;
                rng.Find.Execute(ref findText, ref oTrue, ref oFalse, ref oTrue,
                    ref oFalse, ref oFalse, ref oTrue, ref oFindStop, ref oFalse);
                rng.Select();
                string DDNA = rng.Text;
                rng.Delete();
            }

    However, I would like to have proper tags and search for them, like that: "<<DDNA>*</DDNA>>". Using that expression triggers the error:

    The Find What text contains a Pattern Match expression which is not valid.

    How do I feed the special characters (<>) into find.execute without provoking an error? (I tried "<\<DDNA\>*\</DDNA\>>", that did not work because it is an unrecognized escape sequence)

    Credits to the search method go to Cindy Meister, posted in here.

    As always thanks for all the help,

    Michael

    Wednesday, May 9, 2012 2:10 AM

Answers

All replies

  • I'm not surprised you're having difficulty finding more info on Word regular expressions...It's not well explained. http://office.microsoft.com/en-us/help/add-power-to-word-searches-with-regular-expressions-HA001087305.aspx is a good page I've come across, though you may well have already found it.

    You can put characters in square brackets for literals. So [>]> will search for > at the end of a word.


    • Edited by JosephFox Wednesday, May 9, 2012 9:34 AM
    • Marked as answer by Rinmic Wednesday, May 9, 2012 10:39 PM
    Wednesday, May 9, 2012 9:32 AM
  • Hi Joseph,

    I tried the brackets with these expressions:

    <[<]DDNA[>]*[<]/DDNA[>]>

    <[<DDNA>*</DDNA>]>

    <[<]DDNA[>]*[<][/]DDNA[>]>

    All three did not return an error, but all three failed to select the tags and the text in between, instead all three selected all of the copied content.

    I followed up your link, but sadly they state that one needs to use \ to use special characters, which does not work for me as I described earlier.

    I would  be greatful for any ideas :).

    Thanks for the help,

    Michael

    edit:

    [<]DDNA*/DDNA[>]

    Does the trick, but I it is not as limiting as I wished it to be. But it works. Thanks!

    • Edited by Rinmic Wednesday, May 9, 2012 10:00 PM
    Wednesday, May 9, 2012 9:27 PM
  • Yeah, I should have said, the one thing that appears wrong on that link....the / doesn't help with literals (in my experience, too).

    I would have thought the bottom one, <[<]DDNA[>]*[<][/]DDNA[>]>, would have worked, but clearly I would have been wrong. Like I said, I wish it were documented and explained better, every source I've found has some innacuracies. I remember Office guru Cindy Meister once saying that Word regular expressions were strange compared to others.

    If you need something more specific, I suppose you'll just have to do it 'by hand'....lots of if statements!

    Wednesday, May 9, 2012 10:34 PM