请考虑以下示例:
header-1
item1-1
item1-2
item1-3
header-2
item2-1
item2-2
...我希望它是这样的格式:
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2 item2-1
header-2 item2-2
...我想用正则表达式可以很容易地做到这一点,但我就是想不通
任何正则表达式语法都是受欢迎的,我在Wine下使用RegexBuddy
发布于 2012-01-23 02:15:53
使用RegexBuddy,您可以在两个步骤中完成此操作。
首先搜索(?<=(^\S.*$)(?s:.*?))^\s+并将其全部替换为\1<space>。
这将为您提供
header-1
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2
header-2 item2-1
header-2 item2-2说明:
(?<= # Make sure we're right after the following match:
( # Match and capture in group 1 (the header):
^ # From the start of the line...
\S # but only if the first character is not whitespace
.* # match any number of characters except newlines
$ # until the end of the line (OK, that's redundant).
) # End of group 1
(?s: # Start a non-capturing group, DOTALL mode enabled
.*? # that matches any number of any character, as few as possible.
) # End of group
) # End of lookbehind assertion
^\s+ # Now match one or more whitespace characters at the start of the line然后搜索^(.*)$\r?\n(?=\1)并替换为空字符串。
这会导致
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2 item2-1
header-2 item2-2说明:
^ # Match from the start of the line
(.*) # Match and capture the entire line in group 1
$ # Match until the end of the line (OK, redundant again)
\r?\n # Match a linebreak
(?=\1) # Do all this only if the next line starts with the same string as abovehttps://stackoverflow.com/questions/8963282
复制相似问题