使用正则表达式和PHP分离/分组纯文本

使用正则表达式和PHP分离/分组纯文本,php,r,arrays,json,regex,Php,R,Arrays,Json,Regex,我可以使用正则表达式将数据从下面的纯文本中分离出来。您可以使用正则表达式获取所有开头字符正确的字符串,如190.A或A. \b(?=(?:\d+|[A-Z])\.[A-Z]) \b单词边界 (?=正向前瞻,断言右边的是 (?:\d+|[A-Z])匹配1+位或单个字符A-Z \.[A-Z]匹配和单个字符A-Z )关闭正向前瞻 | 如果在一个数组中有所有这些条目,例如,可以使用获取json输出所需的数组结构 $pattern = "/\b(?=(?:\d+|[A-Z])\.[A-Z])/"

我可以使用正则表达式将数据从下面的纯文本中分离出来。

您可以使用正则表达式获取所有开头字符正确的字符串,如
190.A
A.

\b(?=(?:\d+|[A-Z])\.[A-Z])
  • \b
    单词边界
  • (?=
    正向前瞻,断言右边的是
    • (?:\d+|[A-Z])
      匹配1+位或单个字符A-Z
    • \.[A-Z]
      匹配
      和单个字符A-Z
  • 关闭正向前瞻
|

如果在一个数组中有所有这些条目,例如,可以使用获取json输出所需的数组结构

$pattern = "/\b(?=(?:\d+|[A-Z])\.[A-Z])/";
$result = preg_split($pattern, $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$result = array_reduce($result, function($carry, $item){

    // If the string starts with a digit
    if (ctype_digit(substr($item, 0, 1))) {

        // Create the  questions key
        $carry[] = ["question" => $item];
        return $carry;
    }

    // Get reference to the last added array in $carry
    end($carry);
    $last = &$carry[key($carry)];

    // Create the answers key
    array_key_exists("answers", $last) ? $last["answers"][] = $item : $last["answers"] = [$item];
    return $carry;
}, []);

print_r(json_encode($result))
输出

[
    {
        "question": "190.A 42-year-old male patient has been delivered to a hospital in a grave condition with dyspnea, cough with expectoration of purulent sputum, fever up to 39,5 oC.The ?rst symptoms appeared 3 weeks ago. Two weeks ago, a local therapist diagnosed him wi- th acute right-sided pneumonia. Over the last 3 days, the patient\u2019s condition deteriorated: there was a progress of dyspnea, weakness, lack of appetite. Chest radiography con?rms a rounded shadow in the lower lobe of the right lung with a horizontal?uid level, the right si- nus is not clearly visualized. What is the most likely diagnosis? ",
        "answers": [
            "A.Abscess of the right lung ",
            "B.Acute pleuropneumonia ",
            "C.Right pulmonary empyema ",
            "D.Atelectasis of the right lung ",
            "E.Pleural effusion "
        ]
    },
    {
        "question": "191.An 11-year-old boy complains of general weakness, fever up to 38,2 oC, pain and swelli- ng of the knee joints, feeling of irregular heartbeat. 3 weeks ago, the child had quinsy. Knee joints are swollen, the overlying skin and skin of the knee region is reddened, local temperature is increased, movements are li- mited. Heart sounds are muf?ed, extrasystole is present, auscultation reveals apical systolic murmur that is not conducted to the left ingui- nal region. ESR is 38 mm\/h. CRP is 2+, anti- streptolysin O titre - 40 0. What is the most likely diagnosis? ",
        "answers": [
            "A.Acute rheumatic fever ",
            "B.Vegetative dysfunction ",
            "C.Non-rheumatic carditis ",
            "D.Juvenile rheumatoid arthritis ",
            "E.Reactive arthritis "
        ]
    },
    {
        "question": "192.A 28-year-old male patient complains of sour regurgitation, cough and heartburn that occurs every day after having meals, when bending forward or lying down. These problems have been observed for 4 years. Objective status and laboratory values are normal. FEGDS revealed endoesophagitis. What is the leading factor in the development of this disease? ",
        "answers": [
            "A.Failure of the lower esophageal sphincter ",
            "B.Hypersecretion of hydrochloric acid ",
            "C.Duodeno-gastric re?ux ",
            "D.Hypergastrinemia ",
            "E.Helicobacter pylori infection "
        ]
    },
    {
        "question": "193.On admission a 35-year-old female reports acute abdominal pain, fever up to 38,8 oC, mucopurulent discharges. The pati- ent is nulliparous, has a history of 2 arti?cial abortions. The patient is unmarried, has sexual Krok 2 Medicine 20 14 24 contacts. Gynecological examination reveals no uterus changes. Appendages are enlarged, bilaterally painful. There is profuse purulent vaginal discharge. What study is required to con?rm the diagnosis? ",
        "answers": [
            "A.Bacteriologic and bacteriascopic studies ",
            "B.Hysteroscopy ",
            "C.Curettage of uterine cavity ",
            "D.Vaginoscopy ",
            "E.Laparoscopy"
        ]
    }
]
您可能要做的是使用以正确字符开头的所有字符串,如
190.A
A.

\b(?=(?:\d+|[A-Z])\.[A-Z])
  • \b
    单词边界
  • (?=
    正向前瞻,断言右边的是
    • (?:\d+|[A-Z])
      匹配1+位或单个字符A-Z
    • \.[A-Z]
      匹配
      和单个字符A-Z
  • 关闭正向前瞻
|

如果在一个数组中有所有这些条目,例如,可以使用获取json输出所需的数组结构

$pattern = "/\b(?=(?:\d+|[A-Z])\.[A-Z])/";
$result = preg_split($pattern, $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$result = array_reduce($result, function($carry, $item){

    // If the string starts with a digit
    if (ctype_digit(substr($item, 0, 1))) {

        // Create the  questions key
        $carry[] = ["question" => $item];
        return $carry;
    }

    // Get reference to the last added array in $carry
    end($carry);
    $last = &$carry[key($carry)];

    // Create the answers key
    array_key_exists("answers", $last) ? $last["answers"][] = $item : $last["answers"] = [$item];
    return $carry;
}, []);

print_r(json_encode($result))
输出

[
    {
        "question": "190.A 42-year-old male patient has been delivered to a hospital in a grave condition with dyspnea, cough with expectoration of purulent sputum, fever up to 39,5 oC.The ?rst symptoms appeared 3 weeks ago. Two weeks ago, a local therapist diagnosed him wi- th acute right-sided pneumonia. Over the last 3 days, the patient\u2019s condition deteriorated: there was a progress of dyspnea, weakness, lack of appetite. Chest radiography con?rms a rounded shadow in the lower lobe of the right lung with a horizontal?uid level, the right si- nus is not clearly visualized. What is the most likely diagnosis? ",
        "answers": [
            "A.Abscess of the right lung ",
            "B.Acute pleuropneumonia ",
            "C.Right pulmonary empyema ",
            "D.Atelectasis of the right lung ",
            "E.Pleural effusion "
        ]
    },
    {
        "question": "191.An 11-year-old boy complains of general weakness, fever up to 38,2 oC, pain and swelli- ng of the knee joints, feeling of irregular heartbeat. 3 weeks ago, the child had quinsy. Knee joints are swollen, the overlying skin and skin of the knee region is reddened, local temperature is increased, movements are li- mited. Heart sounds are muf?ed, extrasystole is present, auscultation reveals apical systolic murmur that is not conducted to the left ingui- nal region. ESR is 38 mm\/h. CRP is 2+, anti- streptolysin O titre - 40 0. What is the most likely diagnosis? ",
        "answers": [
            "A.Acute rheumatic fever ",
            "B.Vegetative dysfunction ",
            "C.Non-rheumatic carditis ",
            "D.Juvenile rheumatoid arthritis ",
            "E.Reactive arthritis "
        ]
    },
    {
        "question": "192.A 28-year-old male patient complains of sour regurgitation, cough and heartburn that occurs every day after having meals, when bending forward or lying down. These problems have been observed for 4 years. Objective status and laboratory values are normal. FEGDS revealed endoesophagitis. What is the leading factor in the development of this disease? ",
        "answers": [
            "A.Failure of the lower esophageal sphincter ",
            "B.Hypersecretion of hydrochloric acid ",
            "C.Duodeno-gastric re?ux ",
            "D.Hypergastrinemia ",
            "E.Helicobacter pylori infection "
        ]
    },
    {
        "question": "193.On admission a 35-year-old female reports acute abdominal pain, fever up to 38,8 oC, mucopurulent discharges. The pati- ent is nulliparous, has a history of 2 arti?cial abortions. The patient is unmarried, has sexual Krok 2 Medicine 20 14 24 contacts. Gynecological examination reveals no uterus changes. Appendages are enlarged, bilaterally painful. There is profuse purulent vaginal discharge. What study is required to con?rm the diagnosis? ",
        "answers": [
            "A.Bacteriologic and bacteriascopic studies ",
            "B.Hysteroscopy ",
            "C.Curettage of uterine cavity ",
            "D.Vaginoscopy ",
            "E.Laparoscopy"
        ]
    }
]

谢谢你给我们提供了大量的信息,非常有用!不过还有一个问题,你的预期输出是什么?谢谢你,Emre,你能在你的问题中发布你预期的JSON格式的数据吗(这在这里是典型的,以防链接断开-这样将来的用户也可以访问它!)。不过我们绝对可以帮你。很高兴看到一个新用户努力问他们的问题:)我更新了我的问题。但我认为没有人能帮上忙:)这个问题甚至不在主页上@ctwheels这非常有用,只想确认一下,因为输出是
90
,而不是关闭
190
-我猜是打字错误?@ctwheels是的,打字错误,1号缺失。我还感谢你给我们提供了大量的信息,非常有用!不过还有一个问题,你的预期输出是什么?谢谢你,Emre,你能在你的问题中发布你预期的JSON格式的数据吗(这在这里是典型的,以防链接断开-这样将来的用户也可以访问它!)。不过我们绝对可以帮你。很高兴看到一个新用户努力询问他们的问题:)我更新了我的问题。但是我认为没有人能帮上忙:)这个问题甚至没有在主页上@ctwheels这非常有用,只想确认一下,因为输出是
90
,而不是关闭
190
-我猜是打字错误?@ctwheels是的,打字错误,1号缺失。我补充说