【问题标题】:Getting the useful words from a word list从单词列表中获取有用的单词
【发布时间】:2011-10-23 07:46:18
【问题描述】:

我有以下字符串:

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

对我来说,有效的文本如下

Over/Under 1.5
Over/Under 2.5
Over/Under 3.5
Over/Under 4.5
Over/Under X.X

X.X 是一个数字。

如何判断天气是否是有效字符串?

【问题讨论】:

    标签: php string-matching


    【解决方案1】:

    This es not Over/Under 1.5 匹配吗?如果是这样:

    $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";
      }
    }
    

    如果不是,将 preg_match 更改为

    preg_match('#^Over/Under \d\.\d$#', $word);
    

    就像@Tokk 写的那样,如果字符串应该在 Over OR Under 上匹配,那么您需要更改为 OR - |

    preg_match('#^(Over|Under) \d\.\d$#', $word);
    

    【讨论】:

      【解决方案2】:
      if(preg_match('/Over\/Under \d\.\d/', $str)) {
      
      }
      

      【讨论】:

      • 这是一个不错的解决方案,但我想将此字符串“Half-time Totals Over/Under 0.5”设为假,但我得到了正确的结果。
      【解决方案3】:

      检查它是否与正则表达式匹配

      Over/Under [0-9]*.[0-9]*
      

      (如果是Over OR Under,选择(Over|Under)

      【讨论】:

        【解决方案4】:

        使用正则表达式。我自己不是很擅长,但这对我有用:

        $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 了解更多信息

        【讨论】:

          猜你喜欢
          • 2011-02-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-23
          • 1970-01-01
          相关资源
          最近更新 更多