php preg_match_all,在可用时返回多个可选值

发布于 2024-10-16 09:18:51 字数 721 浏览 2 评论 0原文

大海捞针的例子:

INTERVENTIONS:
---------------------
Med Given: Versed - 9:50 PM Med Admin Route: Intravenous    Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM  Med Admin Route: Intravenous    Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM    Med Admin Route: Intravenous    Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM Dosage: 2.00 L/MIN
Med Given: Vancomycin
Med Given: Fentanyl
Med Given: Dopamine
Med Given: Dextrose
Med Given: Gentamicin

正如你所看到的,有时有时间( - H:MM AM/PM),有时是“药物管理路线:...”和“剂量:...”,我总是想要名称(Versed) 、氧气等)以及如果可用 - 时间(H:MM AM/PM)、途径(静脉注射、口服等)和剂量(20.00 MG、2.00 L/MIN 等)全部存储在数组中。我以为我过去曾经拥有过它,但是当我向它扔一个不同的干草堆时,它失败了......还要注意,有时似乎在变量之间有一个选项卡而不是空格,例如 time-Admin 或管理剂量...

Example of the haystack:

INTERVENTIONS:
---------------------
Med Given: Versed - 9:50 PM Med Admin Route: Intravenous    Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM  Med Admin Route: Intravenous    Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM    Med Admin Route: Intravenous    Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM Dosage: 2.00 L/MIN
Med Given: Vancomycin
Med Given: Fentanyl
Med Given: Dopamine
Med Given: Dextrose
Med Given: Gentamicin

As you cans see, sometimes there are times ( - H:MM AM/PM), sometimes "Med Admin Route: ..." and "Dosage: ...", I always want the name (Versed, Oxygen, etc) and if available - the time (H:MM AM/PM), route (Intravenous, Oral, etc) and dosage (20.00 MG, 2.00 L/MIN, etc) all stored in an array. I've thought that I've had it in the past but when I throw a different haystack at it it fails... Also note that it appears that sometimes there is a tab instead of a space between the variables like time-Admin or Admin-Dosage...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2024-10-23 09:18:51

幸运的是,我在午休期间有一些空闲时间:)

在正则表达式中,表达式后面的 ? 表示它将接受一次或零次出现。每个示例:

preg_match('/^(foo)?bar/', 'foobar'); // 1
preg_match('/^(foo)?bar/', 'bar');    // also 1

在您的情况下,通过正则表达式有点难以做到,但无论如何都是可行的:

preg_match_all('/Med Given: (?<name>[A-Za-z ]+)(- (?<time>[0-9:]+ (AM|PM)))?( +Med Admin Route: (?<route>\w+))?( +Dosage: (?<dosage>.*))?/', $data, $matches);

然后对数组进行后处理:

$result = array();
foreach ($matches['name'] as $key => $name) {
    $result = array('name'=>$name);
    if (!empty($matches['time'][$key])) $result['time'] = $matches['time'][$key];
    if (!empty($matches['route'][$key])) $result['route'] = $matches['route'][$key];
    if (!empty($matches['dosage'][$key])) $result['dosage'] = $matches['dosage'][$key];
    $results[] = $result;
}
print_r($results);

这应该给您:

Array
(
    [0] => Array
        (
            [name] => Versed 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 20.00 MG
        )
    [1] => Array
        (
            [name] => Lidocaine 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 150.00 MG
        )
    [2] => Array
        (
            [name] => Succinylcholine 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 200.00 MG
        )
    [3] => Array
        (
            [name] => Oxygen 
            [time] => 7:23 PM
            [dosage] => 2.00 L/MIN
        )
    [4] => Array
        (
            [name] => Vancomycin
        )
    [5] => Array
        (
            [name] => Fentanyl
        )
    [6] => Array
        (
            [name] => Dopamine
        )
    [7] => Array
        (
            [name] => Dextrose
        )
    [8] => Array
        (
            [name] => Gentamicin
        )
)

这里唯一的问题是“Med Admin Route”位。它必须是单个单词(即:没有空格)。

Luckily for you, I have some time on my hands during my lunch break :)

In regex, ? after an expression means that it will accept one or zero occurences. Per example:

preg_match('/^(foo)?bar/', 'foobar'); // 1
preg_match('/^(foo)?bar/', 'bar');    // also 1

In your case, it is a little hard to do by regex, but feasible anyway:

preg_match_all('/Med Given: (?<name>[A-Za-z ]+)(- (?<time>[0-9:]+ (AM|PM)))?( +Med Admin Route: (?<route>\w+))?( +Dosage: (?<dosage>.*))?/', $data, $matches);

Then post-process the array:

$result = array();
foreach ($matches['name'] as $key => $name) {
    $result = array('name'=>$name);
    if (!empty($matches['time'][$key])) $result['time'] = $matches['time'][$key];
    if (!empty($matches['route'][$key])) $result['route'] = $matches['route'][$key];
    if (!empty($matches['dosage'][$key])) $result['dosage'] = $matches['dosage'][$key];
    $results[] = $result;
}
print_r($results);

This should give you:

Array
(
    [0] => Array
        (
            [name] => Versed 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 20.00 MG
        )
    [1] => Array
        (
            [name] => Lidocaine 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 150.00 MG
        )
    [2] => Array
        (
            [name] => Succinylcholine 
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 200.00 MG
        )
    [3] => Array
        (
            [name] => Oxygen 
            [time] => 7:23 PM
            [dosage] => 2.00 L/MIN
        )
    [4] => Array
        (
            [name] => Vancomycin
        )
    [5] => Array
        (
            [name] => Fentanyl
        )
    [6] => Array
        (
            [name] => Dopamine
        )
    [7] => Array
        (
            [name] => Dextrose
        )
    [8] => Array
        (
            [name] => Gentamicin
        )
)

The only issue here is the "Med Admin Route" bit. It must be a single word (i.e.: no spaces).

绳情 2024-10-23 09:18:51
preg_match_all('~Med Given: ((?:(?!-\s*\d{1,2}:\d{1,2} (?:A|P)M|Med Admin Route:|Dosage:|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((?:(?!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches);

感谢 phpfreaks.com 的工作人员,完成了工作

preg_match_all('~Med Given: ((?:(?!-\s*\d{1,2}:\d{1,2} (?:A|P)M|Med Admin Route:|Dosage:|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((?:(?!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches);

Got the job done thanks to the guys at phpfreaks.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文