简体   繁体   English

正则表达式 - 没有引号的单个单词和引号内的多个单词

[英]Regex - Single Word Without Quotes and Multiple Words Within Quotes

Should be a simple one for Regex gurus.对于正则表达式大师来说应该是一个简单的。 I haven't had any luck playing around on regex101...我在 regex101 上玩得很不走运...

I am using VBA and Access, and I want to clean up the Rich Text;我正在使用 VBA 和 Access,我想清理富文本; 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!谢谢!

You can use您可以使用

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

Here, [0-9]+ matches one or more digits.这里, [0-9]+匹配一位或多位数字。

To solve the main problem you can use要解决您可以使用的主要问题

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

See the regex demo .请参阅正则表达式演示 Details :详情

  • face= - a string face= - 一个字符串
  • (?:"[^"]*"|'[^']*'|\\S+) - a non-capturing group matching (?:"[^"]*"|'[^']*'|\\S+) - 非捕获组匹配
    • "[^"]*"| - " , 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 \\S+ - 一个或多个非空白字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM