diff options
Diffstat (limited to 'pint/formatting.py')
-rw-r--r-- | pint/formatting.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/pint/formatting.py b/pint/formatting.py index a04205d..5a458db 100644 --- a/pint/formatting.py +++ b/pint/formatting.py @@ -455,14 +455,20 @@ def siunitx_format_unit(units, registry): def extract_custom_flags(spec): import re - flag_re = re.compile("(" + "|".join(list(_FORMATTERS.keys()) + ["~"]) + ")") + if not spec: + return "" + + # sort by length, with longer items first + known_flags = sorted(_FORMATTERS.keys(), key=len, reverse=True) + + flag_re = re.compile("(" + "|".join(known_flags + ["~"]) + ")") custom_flags = flag_re.findall(spec) return "".join(custom_flags) def remove_custom_flags(spec): - for flag in list(_FORMATTERS.keys()) + ["~"]: + for flag in sorted(_FORMATTERS.keys(), key=len, reverse=True) + ["~"]: if flag: spec = spec.replace(flag, "") return spec |