正则表达式选择匹配后的下一行中的名称(直到…)

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

Regex select names in next lines after match (until...)

问题

			Match
				Fridolin
				Marten
	Connection
(?<=Match[\r\n]+\t\t?\t?\t?\t?)([ a-zA-Z&#228;&#246;&#252;&#196;&#214;&#220;&#223;&#233;0-9\.-/\-])+
英文:

I have a text file with different levels (scructured by tabs) and I need to select certain values out of it. Here is an example. I tried this for a very long time, but I can't find any solution.

		Connection
			Match
				Fridolin
				Marten
	Connection
			Inventory
						Fill Up
			Fill Up
		Match
			Peter
			Marcus
        Storage
				Room 1
				Room 2
				Room 3
			Match
				Albert
				Jonas
				Hans
	List
	Match
		Peter
		Marcus

I want to select every name in the following lines after "Match" (which has the same amount of tabs in front of it) until the next level (different amount of tabs) starts. In this case I want to select the names that are listed after the word "Match". Until (for example) "Connection" pops up and the amount of tabs in front of it (level) changes. The Names that follow "Match" are always on the same level. I can't use multiline for this.

			Match
				Fridolin
				Marten
	Connection
(?&lt;=Match[\r\n]+\t\t?\t?\t?\t?)([ a-zA-Z&#228;&#246;&#252;&#196;&#214;&#220;&#223;&#233;0-9\.-/\-])+

I have already this regex, which selects at least the first name that follows "Match". I don't know how to select the next names and stop if the level changes.

答案1

得分: 2

以下是代码部分的翻译:

(?<=Match)\n(\s+)\w+(?:\n\w+)+

请注意,这是正则表达式,用于匹配文本中的特定模式。如果需要有关这个正则表达式的更多信息,请让我知道。

英文:

Try this:

(?&lt;=Match)\n(\s+)\w+(?:\n\w+)+

online demo

The regular expression matches as follows:

Node Explanation
(?&lt;= look behind to see if there is:
Match 'Match'
) end of look-behind
\n '\n' (newline)
( group and capture to \1:
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))
) end of \1
\w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible))
(?: group, but do not capture (1 or more times (matching the most amount possible)):
\n '\n' (newline)
\1 what was matched by capture \1
\w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible))
)+ end of grouping

答案2

得分: 1

尝试:

```none
\b匹配\n((\s*).*\n(?:.*(?:\n|\Z))*)

正则表达式演示


这将匹配匹配,后面跟随换行,然后是任意数量的空格作为捕获组1。然后使用此捕获组来匹配其他行。


<details>
<summary>英文:</summary>

Try:

```none
\bMatch\n((\s*).*\n(?:.*(?:\n|\Z))*)

Regex demo.


This will match Match, following newline and then any number of whitespaces as capturing group 1. Then use this capturing group to match other lines.

huangapple
  • 本文由 发表于 2023年2月18日 18:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492868.html
匿名

发表评论

匿名网友

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

确定