正则表达式从匹配中获得最后匹配的匹配项 [英] Regex get last matched occurence from matches

查看:116
本文介绍了正则表达式从匹配中获得最后匹配的匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试在.NET应用程序中编写一个正则表达式来从特定的字符串中获取国家/地区名称而且我能够创建一些东西,但它是不完全是我需要的。我有这种类型的字符串作为输入:



C0230:R1410:S.05.02.01.04:本国 - 生活义务:毛利:前5个国家(按毛保费金额计算) - 非寿险义务:前5个国家(按保费总额计算) - 人寿保险责任:美国



只有当 C0230 R1410 S.05.02 出现在该行中时,我才需要在那里输出国名,否则我不符合国家名称或不会使用它。



我尝试过:



下面的正则表达式给出了三个匹配,最后一个是我感兴趣的,但我正在寻找一个正则表达式中的解决方案,只匹配最后一个。



(?<  =(?i)((C) 0230 )。*((R) 1410 )。*((S)\。 05  02 )。*(生活义务))。[^ \d] * \ b 





有人可以请请问我怎么做?

解决方案

试试这个:

(?< = C0230。 ?+ R1410 + S\.05\.02)(+:?){5}(小于国家> +)

。然后使用Country组来获取您感兴趣的数据。


如果您在结尾处放置一个美元符号,它将只返回最后一个匹配(因为

Hi,

I am trying to write a regex in .NET application to get the country name from a specific string and i am able to make something but it's not completely what i need. I have this type of string line as input:

C0230: R1410: S.05.02.01.04: Home Country - life obligations: Gross: Top 5 countries (by amount of gross premiums written) - non-life obligations: Top 5 countries (by amount of gross premiums written) - life obligations: USA

and i need to get out the country name in there only if the C0230, R1410 and S.05.02 are present in the line, otherwise i will not match the country name or will not use it.

What I have tried:

The following regex is giving three matches back and the last one is which i am interested it, but i am looking for a solution within regex to just only match the last one.

(?<=(?i)((C)0230).*((R)1410).*((S)\.05.02).*(life obligations)).[^\d]*\b



Can somebody please point me to how it can be done?

解决方案

Try this:

(?<=C0230.+?R1410.+S\.05\.02)(.+:){5}(?<Country>.+)


Then use the "Country" group to get the data you are interested in.


If you put a dollar sign at the end, it will only return the last match (because


这篇关于正则表达式从匹配中获得最后匹配的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆