# Word wildcard search expression matching longest possible string having applied a certain character style to it

I am writing a macro to convert a Word document to LaTeX.

All goes well, except one thing: The document makes heavy use of style templates, in effect using character styles to achieve a functional markup. For instance, all author names that appear in the text use the character style "Author name". All non-English words or (and this it where it becomes tricky) phrases have the style "Foreign word" applied to them.

Let's consider the example of the "Foreign word" style. In order to convert it to LaTeX, what I need to do is create a search that searches as greedily as possible for all characters that have the style "Foreign word" applied to them, take this string, and wrap it in \emph{\1} (\1 indicating the position of the matching string).

Using Microsoft's very modest explanation of its wildcard syntax, I would expect that a search for the term "*" (sans the quotes) and an applied style of "Foreign word" should do it, but it does not. It is not greedy enough, only finding individual characters. I can make wildcard searches more greedy by including delimiters in the search --- "[ ]*[ ]" does find entire words --- , but that would fail in the current instance, because, for instance, while a term like "ad-hoc" might be preceded and followed by space, the space itself will not have the "Foreign word" style applied to it and thus be excluded from the search (nevermind the fact that there is a plethora of potential string borders: a multi-word foreign phrase might be preceded and followed by either a colon, semicolon, space, one of six potential quotation mark characters, paragraph mark, or...).

So essentially, I am looking for a wildcard search expression for Word that matches the longest uninterrupted string possible that has a certain style applied to it.

Edited by barlop to add the OP's clarification.

As an example, given this text below, which i've put in quotes.

"Apart from seigneuries, there were also higher noble fiefdoms to be awarded, although nowadays the only remaining title of nobility in Québec that can be traced back to the seigneurial system is the title of ‘Baron de Longueuil’, in the Le Moyne family"

Looking at that text quoted above. All italicised words have the style "Foreign word" applied to them. In the case of "Baron de Longueil", this includes the spaces between "Baron", "de", and "Longueil". I am looking for a search that would catch each of these three italicised terms as one string respectively.

Result should be "Apart from \emph{seigneuries}, there were also higher noble fiefdoms to be awarded, although nowadays the only remaining title of nobility in Québec that can be traced back to the \emph{seigneurial} system is the title of ‘\emph{Baron de Longueuil}’, in the Le Moyne family

• what does "an applied style of 'Foreign word'" mean?! I checked that word help page you linked to, it doesn't even use the word "style" let alone "foreign word". Like, what is a "style" if you mean applying a regex based on font, and whether bold or italic, i'd think that's a vba question. a)test if text is bold b)apply a regex – barlop Jan 2 '14 at 14:14
• oh i think i see.. so you want the regex to consider whether text is bold.. Well.. hmm. – barlop Jan 2 '14 at 14:17
• Can you give an example with a case that demonstrates the problem – barlop Jan 2 '14 at 14:18
• Styles are predefined formats. See office.microsoft.com/en-us/word-help/… You can (both in a VBA macro and in the advanced Find and replace box) limit the search operation to only those paragraphs or strings that have a certain style applied to them. – jjbornheim Jan 2 '14 at 14:18
• ok but for a regular expression to take styles or anything non-regex, into consideration is another matter.. how about you give an example of a case that demonstrates the problem, like. Here is the text/input with any markup.. And here is what you want it to be replaced with. – barlop Jan 2 '14 at 14:20

Here is the principle. This replaces any italic text with some mark up around regular text.

^& means replace with original.

and to specify italic or regular, I chose format..font.. then chose italic or regular from the dropdown within the font dialog box that popped up.

Notice the find box is actually blank, but below it it says the formatting/style it's looking for, (italic)

Notice the replace specifies what to replace with, and below, specifies the formatting/style(regular in this case)

Regarding choosing the font/styles, here is how I did it, with screenshots included.

In the bottom left of the find/replace screen choose format..font

And choose Italic. I ignore all those checkbox like things under 'effects'. It's the selections in that textbox area(italic,regular,..), which i've circled in black that are the ones to use, for selecting regular or italic etc, that worked.

• This beautiful. I didn't know that you can replace with original without using the cumbersome wildcard search. Thank you! – jjbornheim Jan 2 '14 at 14:58
• yep, it is elegant. Word isn't big on syntax! the regex stuff in ms word was not invented by ms. so i figured they hadn't added italic or any style functionality to it.but, I foundthe answer by looking up how to do a simple function like replace italic words. I got a link about changing bold to italic. msofficeforums.com/word/… pretty much said leave find blank and mentioned ^&. a link mentioned format..font So, your question becomes simplified when one looks into how to do something trivial like replace italic with xyz or with bold. – barlop Jan 2 '14 at 15:06