Getting the useful words from a word list
I have the following strings:
Over/Under 1.5
Over/Under 2.5
Over/Under 3.5
Over/Under 4.5
This is not Over/Under 1.5
Other text
For me the valid texts are the following
Over/Under 1.5
Over/Under 2.5
Ov开发者_Python百科er/Under 3.5
Over/Under 4.5
Over/Under X.X
where the X.X
is a number.
How can I make a decision weather is a valid string or not?
Does This es not Over/Under 1.5
match? If so:
$words = array('Over/Under 1.5',
'Over/Under 2.5',
'Over/Under 3.5',
'Over/Under 4.5',
'This es not Over/Under 1.5',
'Other text');
foreach ($words as $word) {
if (preg_match('#.*Over/Under \d\.\d.*#', $word)) {
echo "Match $word\n";
}
}
If not, change the preg_match to
preg_match('#^Over/Under \d\.\d$#', $word);
Like @Tokk writes, if the string should match on Over OR Under, then you need to change to an OR - |
preg_match('#^(Over|Under) \d\.\d$#', $word);
if(preg_match('/Over\/Under \d\.\d/', $str)) {
}
Check if it matches the Regex
Over/Under [0-9]*.[0-9]*
(if it is Over OR Under, choose (Over|Under)
Use a regular expression. I'm not very good at them myself, but this worked for me:
$t
mp = array
(
"Over/Under 1.5",
"Over/Under 2.5",
"Over/Under 3.5",
"Over/Under 4.5",
"Over/Under 5.5",
"fdgdfgdf",
"Other"
);
$tmp2 = array_filter($tmp, function($element) { return preg_match('(Over\/Under [0-9]\.[0-9])', $element); });
print_r($tmp2);
http://php.net/manual/en/function.preg-match.php for more info
精彩评论