Jump to content

Regex - target a single sentence?


Frosty
Go to solution Solved by requinix,

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.

 

 

Edited by Frosty
Link to comment
Share on other sites

  • Solution

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 [^.]*.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.