Jump to content

Regex - target a single sentence?


Frosty

Recommended Posts

As a beginner to regexes I've made some headway in solving some scenarios, but am having an issue with a crafting a regex that does a particular job!

 

So I'm trying to target a single sentence. The regex needs to start with a single capital letter, and then ends at a full stop. But like most sentences, it will include other stuff like: commas, single/double quotes, parenthesis, and all other manner of things.

 

Been using a regex that I stumbed upon that managed to carry out the first object (target), but can't seem to limit it to one.

 

 

Link to comment
https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/
Share on other sites

If you can guarantee that all sentences start with capital letters and that periods only occur at the end of sentences,

Starting at the beginning of the string (which a regex does by default), find a capital letter and go up until the first period.[/code]

[A-Z].*?\.
If you used .*, that will go all the way to the end of the entire string and then backtrack until it finds a period. Using .*? will go just as far as is needed until it can match; here it's like using [^.]*.

Yep, there will be no perfect solution with regex alone. Even if the grammar and punctuation are done perfectly, there are too many alternates and exceptions that involve punctuation, nested stuff, etc. And that's if the grammar and punctuation are perfect - very few people out there are 100% accurate about that stuff, or even 75%. Even if you were to attempt to make a parser that uses more than just regex, it's just not possible to perfectly parse all the exceptions and overcome the fuckups. The human brain is considered an amazing thing for a reason: because it can manage to parse it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.