shaddf Posted October 20, 2015 Share Posted October 20, 2015 how can I make this regex allow punction marks "/^([a-zA-Z\s ]){4,30}$/" At the moment it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/ Share on other sites More sharing options...
Jacques1 Posted October 20, 2015 Share Posted October 20, 2015 You'll have to be a bit more specific. What kind of puncuation marks? Period, comma, semicolon? And where exactly are they allowed? Are you OK with “.......”? Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1523821 Share on other sites More sharing options...
shaddf Posted October 24, 2015 Author Share Posted October 24, 2015 You'll have to be a bit more specific. What kind of puncuation marks? Period, comma, semicolon? And where exactly are they allowed? Are you OK with “.......”? at the end of a sentence, just like in literature i.e period,comma,semicolon,colon,hyphen and question mark Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524214 Share on other sites More sharing options...
printf Posted October 26, 2015 Share Posted October 26, 2015 Something like... "/^[a-z\s]{4,30}+([\.!,;:-?]{1}+)?$/i", Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524396 Share on other sites More sharing options...
Jacques1 Posted October 27, 2015 Share Posted October 27, 2015 (edited) That regex makes absolutely no sense. For example, this will match aaaa< but not This is an English sentence, definitely. And what is the quantifier combination “{1}+” supposed to do? Sentences look more like this: <?php $word = '[a-zA-Z]+'; // a single word $words = "$word( $word)*"; // a sequence of space-separated words $sentence = "$words([,;:] $words)*[.!?]"; // a sentence $sentences = "/$sentence+/"; $testInput = 'This is an English sentence, definitely. And another one.'; var_dump(preg_match($sentences, $testInput)); Edited October 27, 2015 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524455 Share on other sites More sharing options...
Psycho Posted October 27, 2015 Share Posted October 27, 2015 There are many characters that can legitimately be within and at the end of a sentence " . . . just like in literature". If this is for a real-world application that will presumably produce an error for sentences entered by users, you are going to have the very likely possibility of false negatives. Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524459 Share on other sites More sharing options...
printf Posted October 27, 2015 Share Posted October 27, 2015 (edited) My regex makes no sense? I think it's the other way around. The OP asked for a regex that allows for punctuation marks at the end of a sentence, yours allows for repeated punctuation marks anywhere but the beginning of the sentence;;;;;;;:,, which makes no sense to me! If you want to strictly match a English literature sentence, then something like this will work. /^(?:\w+(\, |\; |\: | )){1,}(?:\w+(\.|\!|\?))$/i Edited October 27, 2015 by printf Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524481 Share on other sites More sharing options...
Jacques1 Posted October 27, 2015 Share Posted October 27, 2015 This is, again, nonsense. Please learn the basic syntax of regular expressions before you try to give advice. The \w character class includes the underscore and digits, which means you consider “_ _!” or “123 123.” valid English sentences. This obviously makes no sense. What's the point of “{1,}”? I guess what you're looking for is the + quantifier. Why are you escaping characters that don't require any escaping in the first place? Characters like “,” or “;” or “:” can be written down verbatim, you know? They don't have any meaning in regexes. Why do you require more than one word? “Go!” is a valid sentence, don't you think? Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524493 Share on other sites More sharing options...
Jacques1 Posted October 27, 2015 Share Posted October 27, 2015 Anyway, I agree with Psycho that trying to “validate” text causes more harm than good. The English language is much, much more complex than anything a regex could cover. Of course we can check the basic structure, but this will exclude a large amount of perfectly valid text. Who are we to decide that a text is “invalid”, anyway? People use all kinds of nonstandard language constructs, and that doesn't mean they're all wrong. Assuming that words only consist of a-zA-Z is already a misconception. What about “Raison d'être”? What about “O'Neil”? So unless you have a good reason why you want to annoy your users and force them to use some primitive subset of the English language, just forget about it. Quote Link to comment https://forums.phpfreaks.com/topic/298714-regex-to-allow-text-and-punctuation-marks/#findComment-1524522 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.