Home > database >  Regex - Single Word Without Quotes and Multiple Words Within Quotes
Regex - Single Word Without Quotes and Multiple Words Within Quotes

Time:11-06

Should be a simple one for Regex gurus. I haven't had any luck playing around on regex101...

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

I have this

    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

But 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

Thanks!

CodePudding user response:

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
  • Related