mriksman
mriksman

Reputation: 309

Single Word Without Quotes and Multiple Words Within Quotes

I am using VBA and Access to clean up the Rich Text. I don't mind colors, bold, underline, but I want to force the font style and size.

With objRegEx
    .Global = True
    'Replace font size
    .Pattern = "size=[0-9]"
    strText = .Replace(strText, " size=" & nSize)
    'Replace font face
    .Pattern = "face=([""'])(?:[\r\n]*(?=(\\?))\2.)*?\1"
    strText = .Replace(strText, "face=" & strFont)
End With

It only works if the font is encased in quotes. This doesn't work for single-word-named fonts.

I need to match

font="Times New Roman"
font='Times New Roman'
font=Calibri

Upvotes: 1

Views: 65

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626801

You can use

.Pattern = "size=[0-9]+"

Here, [0-9]+ matches one or more digits.

To solve the main problem you can use

.Pattern = "face=(?:""[^""]*""|'[^']*'|\S+)"

See the regex demo. Details:

  • face= - a string
  • (?:"[^"]*"|'[^']*'|\S+) - a non-capturing group matching
    • "[^"]*"| - ", then any zero or more chars other than " and then a " char, or
    • '[^']*'| - ', then any zero or more chars other than ' and then a ' char, or
    • \S+ - one or more non-whitespace chars

Upvotes: 1

Related Questions