David Stampher David Stampher -4 years ago 44
Vb.net Question

VB.Net Regular Expression, get words which may contain single quotes, that are between single quotes

I have a list of countries, each country name is between single quotes, like so

'United States'


I am using this Regular Expression to match them:
'(.*?)'


Works great, until I get to a more obscure country name like
'Cote d'Ivoire'
, it ends up matching Cote d

How can I change my current regular expression to properly match country names such as the one I just gave as an example?

Answer Source

You may try to force the regex engine to match the final ' at a non-word boundary position:

'(.*?)'\B

See the regex demo

You may match the first ' when it is preceded with a non-word boundary and followed with a word boundary and similarly match the closing ':

\B'\b(.*?)\b'\B

See another regex demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download