简体   繁体   中英

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
Over/Under 3.5
Over/Under 4.5
Over/Under X.X

where the XX is a number.

How can I make a decision weather is a valid string or not?

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)

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM