Helpful Information
 
 
Category: Regex Programming
RegEx hell - help appreciated

Hi all

I'm struggling with a regular expression. I need to match:

1) The name of a place
2) Then any of [.,: -]
3) Then either nothing or a word that is NOT one of the following three words: oblast, region, krai.

So for example it would match:

"Investment into Chelyabinsk is up on 2007"
"...factory in Chelyabinsk."

But not...

"Investment into Chelyabinsk oblast is up on 2007"
"...factory in Chelyabinsk oblast."

I've looked through lots of guides and found all sorts of info on exceptions (^) and alternatives (|) but can't seem to nail it.

Thanks in advance.

If you're using a regex language that supports it, a lookahead is the construct you need. In perl that would look something like:

/Chelyabinsk[.,: -](?!oblast)/

Otherwise you'll probably have to capture the next word and check if it's valid.

Thanks - but how can I do that in PHP?

It's OK, I improvised and it works, thanks again.

preg_match("/$placename*+(?! oblast| region| krai| okrug)/i", $text)










privacy (GDPR)