Jump to content

Regex for White Space OR Punctuation


member123

Recommended Posts

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.

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.

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.

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

<?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

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.