Frosty Posted October 17, 2013 Share Posted October 17, 2013 (edited) 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 October 17, 2013 by Frosty Quote Link to comment https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/ Share on other sites More sharing options...
Solution requinix Posted October 17, 2013 Solution Share Posted October 17, 2013 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 [^.]*. Quote Link to comment https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/#findComment-1454324 Share on other sites More sharing options...
AbraCadaver Posted October 17, 2013 Share Posted October 17, 2013 So why not use [^.]* so that you match newlines. Or [A-Z].*?\. needs the s modifier. Quote Link to comment https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/#findComment-1454330 Share on other sites More sharing options...
Frosty Posted October 17, 2013 Author Share Posted October 17, 2013 Many thanks you two! This works fine for the most part, although it hits a snag when the text contains either "No." (as in number), and percentages like "8.25%". Quote Link to comment https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/#findComment-1454354 Share on other sites More sharing options...
.josh Posted October 17, 2013 Share Posted October 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283059-regex-target-a-single-sentence/#findComment-1454364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.