Regex以匹配从特定字符串开始的所有行。

时间:2022-09-13 16:31:41

I have this very long cfg file, where I need to find the latest occurrence of a line starting with a specific string. An example of the cfg file:

我有这个很长的cfg文件,其中我需要查找以特定字符串开头的行的最新情况。cfg文件的一个示例:

...
# format: - search.index.[number] = [search field]:element.qualifier
...    
search.index.1 = author:dc.contributor.*
...
search.index.12 = language:dc.language.iso
...
jspui.search.index.display.1 = ANY
...

I need to be able to get the last occurrence of the line starting with search.index.[number] , more specific: I need that number. For the above snippet, that number would be 12.

我需要能够获得以search.index开头的行的最后一次出现。更具体地说,我需要那个号码。对于上面的代码片段,这个数字将是12。

As you can see, there are other lines too containing that pattern, but I do not want to match those.

如您所见,还有其他行也包含该模式,但我不想匹配它们。

I'm using Groovy as a programming/scripting language.

我使用Groovy作为一种编程/脚本语言。

Any help is appreciated!

任何帮助都是赞赏!

3 个解决方案

#1


1  

Have you tried:

你有试过:

def m = lines =~ /(?m)^search\.index\.(\d+)/
m[ -1 ][ 1 ]

#2


1  

Try this as your expression :

试试这个作为你的表达方式:

^search\.index\.(\d+)/

And then with Groovy you can get your result with:

然后用Groovy你可以得到你的结果:

 matcher[0][0]

Here is an explanation page.

这是一个解释页面。

#3


1  

I don't think you should go for it but...
If you can do a multi-line search (anyway you have to here), the only way would be to read the file backward. So first, eat everything with a .* (om nom nom)(if you can make the dot match all, (?:.|\s)* if you can't). Now match your pattern search\.index\.(\d+). And you want to match this pattern at the beginning of a line: (?:^|\n) (hoping you're not using some crazy format that doesn't use \n as new line character).

我认为你不应该这样做,但是……如果可以进行多行搜索(无论如何必须在这里),惟一的方法就是向后读取文件。所以,首先,用一个。* (om nom nom糯)(如果你能让点匹配全部,(?:|\s)*如果你做不到的话。)现在匹配你的模式搜索\.index\ (\d+)。你想匹配该模式在一行的开头:(?:^ | \ n)(希望你不使用一些疯狂的格式,不使用\ n作为新行字符)。

So...

所以…

(?:.|\s)*(?:^|\n)search\.index\.(\d+)

The number should be in the 1st matching group. (Test in JavaScript)

数字应该在第一个匹配组。在JavaScript(测试)

PS: I don't know groovy, so sorry if it's totally not appropriate.

我不知道groovy,如果它完全不合适的话,我很抱歉。

Edit:
This should also work:

编辑:这也应该有效:

search\.index\.(\d+)(?!(?:.|\s)*?(?:^|\n)search\.index\.\d+)

#1


1  

Have you tried:

你有试过:

def m = lines =~ /(?m)^search\.index\.(\d+)/
m[ -1 ][ 1 ]

#2


1  

Try this as your expression :

试试这个作为你的表达方式:

^search\.index\.(\d+)/

And then with Groovy you can get your result with:

然后用Groovy你可以得到你的结果:

 matcher[0][0]

Here is an explanation page.

这是一个解释页面。

#3


1  

I don't think you should go for it but...
If you can do a multi-line search (anyway you have to here), the only way would be to read the file backward. So first, eat everything with a .* (om nom nom)(if you can make the dot match all, (?:.|\s)* if you can't). Now match your pattern search\.index\.(\d+). And you want to match this pattern at the beginning of a line: (?:^|\n) (hoping you're not using some crazy format that doesn't use \n as new line character).

我认为你不应该这样做,但是……如果可以进行多行搜索(无论如何必须在这里),惟一的方法就是向后读取文件。所以,首先,用一个。* (om nom nom糯)(如果你能让点匹配全部,(?:|\s)*如果你做不到的话。)现在匹配你的模式搜索\.index\ (\d+)。你想匹配该模式在一行的开头:(?:^ | \ n)(希望你不使用一些疯狂的格式,不使用\ n作为新行字符)。

So...

所以…

(?:.|\s)*(?:^|\n)search\.index\.(\d+)

The number should be in the 1st matching group. (Test in JavaScript)

数字应该在第一个匹配组。在JavaScript(测试)

PS: I don't know groovy, so sorry if it's totally not appropriate.

我不知道groovy,如果它完全不合适的话,我很抱歉。

Edit:
This should also work:

编辑:这也应该有效:

search\.index\.(\d+)(?!(?:.|\s)*?(?:^|\n)search\.index\.\d+)