How to find a pair of lines that should always start with a hyphen but one of the lines does not start with a hyphen by mistake

huangapple go评论50阅读模式
英文:

How to find a pair of lines that should always start with a hyphen but one of the lines does not start with a hyphen by mistake

问题

I found a pair of lines that should always start with a hyphen but one of those lines does not start with a hyphen by mistake in the provided text. Here's the translation:

在提供的文本中,我找到了一对应该始终以连字符开头的行,但其中一行错误地没有以连字符开头。

英文:

How to search for pairs of lines that should always start with a hyphen but one of those lines does not start with a hyphen by mistake.

I want to find all pairs of rows marked in green:
EXAMPLE

Example text:

491
00:23:34,260 --> 00:23:35,340
- Are you?
- Yes, I am.

492
00:23:34,260 --> 00:23:35,340
- Are you?
Yes, I am.

493
00:23:34,260 --> 00:23:35,340
Are you?
- Yes, I am.

494
00:23:34,260 --> 00:23:35,340
Are you?
Yes, I am.

Some of my attempts. They gave some results, but never quite the desired ones.

\-\ [a-zA-Z0-9]+(\n|.)*?\-\

\-\ [a-zA-Z0-9]+(\n|.)*?^((?!\-).)*$

\-\ [a-zA-Z0-9]+(\n|.)*?(^((?!\-).))

答案1

得分: 2

Here's the translated content:

如果我要思考得非常基础,也许以下内容就足够了:

  • ^ - 开始行锚点;
  • (?!-.+\n-) - 负向先行断言,用于确认位置后面没有紧跟着一个连字符、1个或多个非换行字符、一个换行字符和另一个连字符;
  • .+\n.+ - 匹配1个或多个非换行字符、一个换行字符,然后再次匹配1个或多个非换行字符;
  • (?=\n\n|\Z) - 正向先行断言,用于确认位置后面跟着两个连续的换行字符 或者 文本的结尾。

这将简单地突出显示不包含两个连字符的行集合。然而,在您的问题中,您提到希望突出显示那些缺少一个连字符的集合。这可能意味着您实际上需要其中两行仍然以连字符开头之一。如果是这样的情况,请尝试:

  • ^(?!-.+\n-)(?=-|.+\n-).+\n.+(?=\n\n|\Z)

唯一的区别是正向先行断言,用于确认直接后面有一个连字符或者在一行文本之后有一个连字符。查看在线 演示

英文:

If I was to think very basal, maybe the following would be enough:

^(?!-.+\n-).+\n.+(?=\n\n|\Z)

See an online demo

  • ^ - Start-line anchor;
  • (?!-.+\n-) - Negative lookahead to assert position is not directly followed by a hyphen, 1+ characters other than newline, a newline character and another hyphen;
  • .+\n.+ - Match 1+ characters other than newline, a newline character and again 1+ non-newline characters;
  • (?=\n\n|\Z) - A positive lookahead to assert position is followed by two consecutive newline characters or the end of the text.

This would simply highlight the set of rows that does not include two hyphens. However, in your question you do mention you wish to highlight those sets that have one hyphen missing. Implicitly this could mean you need one of these two lines to actually still start with an hyphen. If that is the case, try:

^(?!-.+\n-)(?=-|.+\n-).+\n.+(?=\n\n|\Z)

The only difference is a positive lookahead to assert that there is a hyphen directly or after one line of text. See an online demo

huangapple
  • 本文由 发表于 2023年4月17日 20:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定