Regular Expressions 101

Community Patterns

ngettext regex

0

Regular Expression
Python

r"
(?:lazy_)?ngtxt\(([\"\'])((?:(?!\1)[^\\]|\\.)+)\1,\s*([\"\'])((?:(?!\3)[^\\]|\\.)+)\3,\s*(?:.+?)\)
"
gm

Description

Matches strings within a ngettext call. Covers the following examples:

  • ngtxt('i am in single-quotes', 'i am in single-quotes 2', foo)
  • ngtxt('i have ' an escaped single-quote within single-quotes', 'i have ' an escaped single-quote within single-quotes 2', foo)
  • ngtxt('i have a "double-quote" in single-quotes', 'i have a "double-quote" in single-quotes 2', foo)
  • ngtxt("i am in double-quotes", "i am in double-quotes 2", foo)
  • ngtxt("i am in double-quotes with single 'quotes'", "i am in double-quotes with single 'quotes' 2", foo)
  • ngtxt('i have an escaped double-quote " within double-quotes', 'i have an escaped double-quote " within double-quotes 2', foo)
  • ngtxt('i am singular in single-quotes', "i am plural in double-quotes", foo)

ngettext has been aliased to ngtxt for my project, so change this to whatever alias you're using. I also have a lazy version of ngettext, hence the optional lazy_ portion, which can be deleted.

Submitted by anonymous - 4 years ago