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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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