regex 拆分. clang格式文件中的长正则表达式

yqkkidmi  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(119)

bounty将在5天后过期。回答此问题可获得+50声望奖励。αλεχολυτ希望吸引更多人关注此问题。

我使用clang格式来排序和分组包含文件。由于一些遗留的原因,我必须在IncludeCategories部分使用足够长的正则表达式。样式选项描述here

IncludeCategories:
  - Regex:           '^"(very|long|regex|pattern|here)/'
    Priority:        2

是否有一种(文档化的)方法可以将这样的正则表达式拆分成多行(而不需要创建具有相同优先级的重复部分)?

2lpgd968

2lpgd9681#

您是否尝试在正则表达式中所需的位置插入新行?

我就是这么做的,而且在我的例子中它是有效的。也许有一些额外的限制,我的例子足够简单,所以它是有效的。为了测试这个问题,我从Github上的一个开源项目中复制了一个. clang格式,并对其进行了轻微的修改。
初始. clang格式:

IncludeCategories:
  - Regex:           '^("boost/first.hpp")'
    Priority:        1
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^("boost/second.hpp")'
    Priority:        2
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^<(algorithm|any|array|atomic|barrier|bit|bitset|cassert|cctype|cerrno|cfenv|cfloat|charconv|chrono|cinttypes|climits|clocale|cmath|codecvt|compare|complex|concepts|condition_variable|coroutine|csetjmp|csignal|cstdarg|cstddef|cstdint|cstdio|cstdlib|cstring|ctime|cuchar|cwchar|cwctype|deque|exception|execution|filesystem|format|forward_list|fstream|functional|future|initializer_list|iomanip|ios|iosfwd|iostream|istream|iterator|latch|limits|list|locale|map|memory|memory_resource|mutex|new|numbers|numeric|optional|ostream|queue|random|ranges|ratio|regex|scoped_allocator|semaphore|set|shared_mutex|source_location|span|sstream|stack|stdexcept|stop_token|streambuf|string|string_view|strstream|syncstream|system_error|thread|tuple|typeindex|typeinfo|type_traits|unordered_map|unordered_set|utility|valarray|variant|vector|version)>'
    Priority:        3
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h>'
    Priority:        4
    SortPriority:    0
    CaseSensitive:   true

初始源代码排序:

#include "boost/first.hpp"
#include "boost/second.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>
#include <unordered_set>
#include <vector>
#include <stddef.h>

已修改.clang-format**(请注意,有几个换行符,并且该正则表达式的优先级已从3更改为1)**:

IncludeCategories:
  - Regex:           '^("boost/first.hpp")'
    Priority:        1
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^("boost/second.hpp")'
    Priority:        2
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^<(algorithm|any|array|atomic|barrier|bit|bitset|cassert|cctype|cerrno|cfenv|cfloat|charconv|chrono|cinttypes|climits|clocale|cmath|
                        codecvt|compare|complex|concepts|condition_variable|coroutine|csetjmp|csignal|cstdarg|cstddef|cstdint|cstdio|cstdlib|cstring|ctime|
                        cuchar|cwchar|cwctype|deque|exception|execution|filesystem|format|forward_list|fstream|functional|future|initializer_list|iomanip|ios|
                        iosfwd|iostream|istream|iterator|latch|limits|list|locale|map|memory|memory_resource|mutex|new|numbers|numeric|optional|ostream|queue|
                        random|ranges|ratio|regex|scoped_allocator|semaphore|set|shared_mutex|source_location|span|sstream|stack|stdexcept|stop_token|streambuf|
                        string|string_view|strstream|syncstream|system_error|thread|tuple|typeindex|typeinfo|type_traits|unordered_map|unordered_set|utility|
                        valarray|variant|vector|version)>'
    Priority:        1
    SortPriority:    0
    CaseSensitive:   true
  - Regex:           '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h>'
    Priority:        4
    SortPriority:    0
    CaseSensitive:   true

修改了源代码顺序**(注意boost/second.hpp是如何被包含在所有包含之后的,现在高优先级更高)**:

#include "boost/first.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>
#include <unordered_set>
#include <vector>
#include "boost/second.hpp"
#include <stddef.h>

相关问题