Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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


Can somebody please point me to how it can be done?
Posted
Updated 10-Jan-18 10:28am
v2

Try this:
(?<=C0230.+?R1410.+S\.05\.02)(.+:){5}(?<Country>.+)$
Then use the "Country" group to get the data you are interested in.
 
Share this answer
 
Comments
Ehsan Sajjad 10-Jan-18 10:00am    
the problem is country name can be any
OriginalGriff 10-Jan-18 10:15am    
Yes - and it matches any.

Or do you mean it can be anywhere?
Ehsan Sajjad 11-Jan-18 7:44am    
never mind, this works for the example posted in question but i was actually looking for something more generic, like your regex is looking for 5 occurrences of : sign and it would not be exactly 5 in every case, can be more or less, but country would be at end.
OriginalGriff 11-Jan-18 7:51am    
Then either change the {5} to a simpler "+" or "*" and give it a try, or better, use the regex to identify the rows you need to process, and use LastIndexOf to get the final data. The more complex you make a regex, the harder it is to understand and modify when the data changes - so a "combination" approach can produce a lot better and more reliable code in the long term.
If you put a dollar sign at the end, it will only return the last match (because $ means "end of the line" in a regular expression).

When I tried this out, it matched ": USA" rather than "USA" and to fix that, you can add .*: (note the space after the colon) to the end of the (?<= ) group, and then you can replace the whole .[^\d]*\b part by \w+ (\w is a "word character").

So, all this together gives:
(?<=(?i)((C)0230).*((R)1410).*((S)\.05.02).*(life obligations).*: )\w+$


(Or, an alternative without regex: check that the string contains C0230 and everything else, then split on ": " and grab the last element of the result array)
 
Share this answer
 
Comments
Ehsan Sajjad 10-Jan-18 10:02am    
i am limited here to only use regex, your suggestion would help i think, do you mean putting $ will is forcing it to just grap the end of line match ?
Thomas Daniels 10-Jan-18 10:04am    
$ means "end of the line", so this will match a sequence of "word characters" that's immediately followed by the end of the line.
Ehsan Sajjad 10-Jan-18 10:08am    
ok great, so let's say in another example i don't have country name at end, in that case this would fail ?
Thomas Daniels 10-Jan-18 10:09am    
Yes, it would fail - I made this regex specifically for strings with the country name at the end, because that was your example.
Ehsan Sajjad 10-Jan-18 10:15am    
ok, thanks for suggesting the $ thing again, i think this will keep me going for now and your answer is valid as per my need for now, so accepting it, thanks once again :)
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900