member123 Posted May 16, 2008 Share Posted May 16, 2008 Can someone help me develop a regex that will work for a word followed by either whitespace or a punctuation mark? Thanks. Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/ Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 wrong section, try the regex section also if you need help with something we need to see what you have so far! also what punctuation marks ? heres an example (\w) hyphenated words example (\w+(?:-)?(?:\w+)?(?:\.|\?|\w|\')+) Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542718 Share on other sites More sharing options...
member123 Posted May 16, 2008 Author Share Posted May 16, 2008 Right now I have preg_match('/' . preg_quote($variant, '/') . '/', $shebang I am looking for words that are used as $variant in a block of text. I also used preg_match('/' . preg_quote($variant, ' /') . '/', $shebang and it worked nicely, except it can't spot the words if they are at the end of a sentence or in front of a comma or whatever because of the ' /' section. I would like to modify it so that it words with a space or a comma, period, exclamation point, question mark, etc. Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542719 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 try this preg_match('/' . preg_quote($variant, '/') . '(?:\s|\?|,|!|\'|"|\.)?/', $shebang Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542730 Share on other sites More sharing options...
member123 Posted May 16, 2008 Author Share Posted May 16, 2008 try this preg_match('/' . preg_quote($variant, '/') . '(?:\s|\?|,|!|\'|"|\.)?/', $shebang Thanks for the help MadTechie. However, I'm still getting the same problem, even with that code. If $variant is App, words like Apple still get picked up. Also should any of those backslashes be double backslashes? Thanks. Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542744 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 ahh okay try this preg_match('/^' . preg_quote($variant, '/') . '(?:\s|\?|,|!|\'|"|\.)?$/', $shebang Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542747 Share on other sites More sharing options...
member123 Posted May 16, 2008 Author Share Posted May 16, 2008 ahh okay try this preg_match('/^' . preg_quote($variant, '/') . '(?:\s|\?|,|!|\'|"|\.)?$/', $shebang I got it working by getting rid of that question mark at the end and having a double backslash in front of the s. Thanks for the help. I'm still not exactly clear on where the double backslashes are needed. Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542750 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 without last ? does the end word work ? whats the problem with the backslashes ? Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542751 Share on other sites More sharing options...
effigy Posted May 16, 2008 Share Posted May 16, 2008 '/^' . preg_quote($variant) . '(?=\s|\p{P}|\z)/' Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542868 Share on other sites More sharing options...
member123 Posted May 16, 2008 Author Share Posted May 16, 2008 For some reason that code gave me a lot of "unknown modifier" errors. Can you please tell me what the difference is between that and the above code? Thanks. Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-542986 Share on other sites More sharing options...
effigy Posted May 16, 2008 Share Posted May 16, 2008 It encompasses punctuation in the complete sense, and, in my opinion, is cleaner. <pre> <?php $tests = array( 'Word', 'Word ', 'Word!', 'Word, etc.', 'Wordxyz', ); $variant = 'Word'; $pattern = '/^' . preg_quote($variant) . '(?=\s|\p{P}|\z)/'; foreach ($tests as $test) { echo "$test: ", preg_match($pattern, $test) ? 'OK' : 'Invalid', '<br>'; } ?> </pre> Word: OK Word : OK Word!: OK Word, etc.: OK Wordxyz: Invalid Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-543059 Share on other sites More sharing options...
corbin Posted May 17, 2008 Share Posted May 17, 2008 <?php $words = array( 'Corbin', '!Corbin!', 'Corbin1', 'Corbin was here', "Corbin\r\n", "Corbin ", "I am Corbin", ); $search = "Corbin"; $search_e = preg_quote($search); foreach($words as $word) { if(preg_match("/\b{$search_e}\b/i", $word)) { echo "{$word} is valid.\r\n"; } } ?> /* Corbin is valid. !Corbin! is valid. Corbin was here is valid. Corbin is valid. Corbin is valid. I am Corbin is valid. */ Will a simple word boundry work? http://www.regular-expressions.info/wordboundaries.html Link to comment https://forums.phpfreaks.com/topic/105901-regex-for-white-space-or-punctuation/#findComment-543711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.