首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在后续行上的扩展匹配

在后续行上的扩展匹配
EN

Stack Overflow用户
提问于 2012-01-23 02:05:31
回答 1查看 91关注 0票数 1

请考虑以下示例:

代码语言:javascript
复制
header-1
    item1-1
    item1-2
    item1-3
header-2
    item2-1
    item2-2
...

我希望它是这样的格式:

代码语言:javascript
复制
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2 item2-1
header-2 item2-2
...

我想用正则表达式可以很容易地做到这一点,但我就是想不通

任何正则表达式语法都是受欢迎的,我在Wine下使用RegexBuddy

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-23 02:15:53

使用RegexBuddy,您可以在两个步骤中完成此操作。

首先搜索(?<=(^\S.*$)(?s:.*?))^\s+并将其全部替换为\1<space>

这将为您提供

代码语言:javascript
复制
header-1
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2
header-2 item2-1
header-2 item2-2

说明:

代码语言:javascript
复制
(?<=   # 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)并替换为空字符串。

这会导致

代码语言:javascript
复制
header-1 item1-1
header-1 item1-2
header-1 item1-3
header-2 item2-1
header-2 item2-2

说明:

代码语言:javascript
复制
^       # 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 above
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8963282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档