Can regex match all the words outside quotation marks?

前端 未结 4 1635
执念已碎
执念已碎 2021-01-06 21:25

I recently typed an essay for my lit class, and my teacher specifically stated a word limit that does not include quotations from the piece. And I thought, why not make a sc

4条回答
  •  庸人自扰
    2021-01-06 21:41

    Depending on the requirements, could use The Greatest Regex Trick Ever

    "[^"]*"|(\w+)
    

    And count the matches of the first capture group.

    \w+ matches one or more word characters.

    See test at regex101.com


    Also skip single quoted strings:

    "[^"]*"|'[^']*'|(\w+)
    

    test at regex101

提交回复
热议问题