如何使用正则表达式提取文本?

时间:2013-01-02 00:10:10

标签: php regex arrays preg-match-all

如何从正则表达式的一组问题中提取问题和选择并回答

示例:

1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse

我需要这样的输出:

$question[0] = "An FMS system, besides controlling navigation, thrust and auto-nav,also provides:";
$choice1[0] = "take-off and landing warnings";
$choice2[0] = "dedicated status and warnings";
$choice3[0] = "GPWS warnings";

$question[1] = "EADI sky and ground display is provided by:";
$choice1[1] = "synthetic TV signals";
$choice2[1] = "raster scan";
$choice3[1] = "stroke pulse";

*符号表示回答 我应该写什么模式 非常感谢

4 个答案:

答案 0 :(得分:3)

$string = '1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse';

$qa = array();
$string = explode("\n\n", $string);
foreach ($string as $set)
{
    $set = preg_split('/(\:?\s(\* )?[abc]\) )/', $set);
    $qa[] = array('question' => ltrim(strstr($set[0], ' ')), 
        'choice1' => $set[1],  'choice2' => $set[2],  'choice3' => $set[3]);
}

print_r($qa);

输出:

Array
(
    [0] => Array
        (
            [question] => An FMS system, besides controlling navigation, thrust and auto-nav,
also provides
            [choice1] => take-off and landing warnings
            [choice2] => dedicated status and warnings
            [choice3] => GPWS warnings
        )

    [1] => Array
        (
            [question] => EADI sky and ground display is provided by
            [choice1] => synthetic TV signals
            [choice2] => raster scan
            [choice3] => stroke pulse
        )

)

答案 1 :(得分:0)

  1. 在空行上爆炸以获得每个问题+答案。
  2. 其中每一个都在:换行符上爆炸,最多两部分,分成问题和答案。
  3. 在换行符时分解答案以获得答案。
  4. 完成。使用的PHP函数:explode,手册附带了更多示例,我不能在这里给出更好的答案。

答案 2 :(得分:0)

你可以像这样在数组中得到你的答案:

 $text='1. An FMS system, besides controlling navigation, thrust and auto-nav,
      also provides:
      a) take-off and landing warnings
      * b) dedicated status and warnings
      c) GPWS warnings

      2. EADI sky and ground display is provided by:
      a) synthetic TV signals
      * b) raster scan
      c) stroke pulse';



        $arr=explode("\n",$text);
        $correctanswer=array();
        $replacements=array('*','a)','b)','c)');
        $questions=array();
        $answers=array();
        $x=0;
        foreach($arr as $ar){
       if(preg_match('#[0-9]#',$ar)){
       $questions[$x][]=preg_replace('#[0-9]\\.#','',$ar);
       $x++;
         }

       if(preg_match('#[*]#',$ar)){
        $correctanswer[$x-1][]=str_replace($replacements,'',$ar);
         }
        $answers[$x-1][]=str_replace($replacements,'',$ar);
          }
      print_r($questions);
       echo '<br/>';
      print_r($correctanswer);

打印:

    Array
       (
       [0] => Array
           (
             [0] =>  An FMS system, besides controlling navigation, thrust and auto-     nav,
             )

      [1] => Array
            (
                [0] =>  EADI sky and ground display is provided by:
              )

            )
        Array
           (
            [0] => Array
                  (
                       [0] =>   dedicated status and warnings
                  )

            [1] => Array
                  (
                   [0] =>   raster scan
                   )

            )

答案 3 :(得分:0)

// Set up some variables
$question = $choice1 = $choice2 = $choice3 = $correct = array();

$string = '1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse';

// Break question string into an array of questions with answers
$data = explode("\r\n\r\n", $string);

// Iterate through each question with answers
foreach ($data AS $i => $tmp)
{
    // Extract question and answers
    preg_match('/\d+\. (.*):\r\n(\*?)\ ?[abc]\) (.*)\r\n(\*?)\ ?[abc]\)\ (.*)\r\n(\*?)\ ?[abc]\)\ (.*)/s', $tmp, $matches);

    // Assign matches to variables
    $question[$i] = $matches[1];
    $choice1[$i] = $matches[3];
    $choice2[$i] = $matches[5];
    $choice3[$i] = $matches[7];

    // Figure out which answer is correct
    if ($matches[2] == '*')
    {
        $correct[$i] = 1;
    }
    else if ($matches[4] == '*')
    {
        $correct[$i] = 2;
    }
    else if ($matches[6] == '*')
    {
        $correct[$i] = 3;
    }
}

var_dump($question, $choice1, $choice2, $choice3, $correct);

输出:

// $question
array(2) {
  [0]=>
  string(82) "An FMS system, besides controlling navigation, thrust and auto-nav,
also provides"
  [1]=>
  string(42) "EADI sky and ground display is provided by"
}
// $choice1
array(2) {
  [0]=>
  string(29) "take-off and landing warnings"
  [1]=>
  string(20) "synthetic TV signals"
}
// $choice2
array(2) {
  [0]=>
  string(29) "dedicated status and warnings"
  [1]=>
  string(11) "raster scan"
}
// $choice3
array(2) {
  [0]=>
  string(13) "GPWS warnings"
  [1]=>
  string(12) "stroke pulse"
}
// $correct
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(2)
}