You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

模式匹配中绕过可选字段:Perl正则提取球员信息异常排查

Fixing Your Perl Regex for Missing Closing TD Tags

The issue with your original regex is that \d+?\S+? doesn’t properly handle the optional closing </td> tag after the player number—especially for single-digit numbers, where the lazy \d+? might not capture the full number, and \S+? can’t reliably skip between the number and the next <td> in both cases (with or without the closing tag).

Here’s the corrected regex that handles both normal and abnormal entries, without capturing the optional closing tag:

while($content =~ /<tr><td>\d+(?:<\/td>)?<td>(.*?)\s-.*?<\/td><td>(\d+?)<\/td>/g) { 
    my $player = $1; 
    my $total = $2; 
    print "\nPlayer => $player Total => $total\n"; 
}

Key Changes Explained:

  • Replaced \d+?\S+?<td> with \d+(?:<\/td>)?<td>:
    • \d+: Greedily matches one or more digits (works for single-digit, two-digit, etc., player numbers).
    • (?:<\/td>)?: A non-capturing optional group (the ?: avoids creating an extra capture) that matches the closing </td> tag only if it exists. The ? makes this group optional, so it handles both cases where the tag is present or missing.
    • <td>: Matches the opening tag for the player name column, which is consistent in both entry types.

Testing with Your Examples:

  1. Abnormal entry: <tr><td>10<td>MANNY MACHADO - FA</td><td>37</td></tr>
    • The regex matches <tr><td>10<td>, skips the missing closing tag, captures MANNY MACHADO as the player, and 37 as the total.
  2. Normal entry: <tr><td>107</td><td>ALEDMYS DIAZ - HOU</td><td>18</td></tr>
    • The regex matches <tr><td>107</td><td>, includes the closing tag in the optional group, captures ALEDMYS DIAZ as the player, and 18 as the total.

This approach explicitly accounts for the optional closing tag without unnecessary captures, and works reliably for all player number lengths.

内容的提问来源于stack exchange,提问作者James Finnegan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:57:16