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

正则表达式匹配特定字符串异常,请求排查修正方案

Regex Fix for Targeted Matching

Problem Analysis

The original regex fails on q.00.00.000.0.xx_0-1111 because the (?<=[^-\s]{7}) lookbehind triggers at the earliest valid position (right after q.00.00), resulting in the match .000.0.xx_0. The intended match .0.xx_0 would be captured by the first lookbehind ((?<=[^-\s]{4}.\d{3})), but regex prioritizes leftmost matches, so the earlier, incorrect match is selected.

Corrected Regex

(?:(?<=\.\d{3})[^-]+(?=-))|(?:(?!.*?\.\d{3}(?=-))(?<=[^-\s]{7})[^-]+(?=-))|(?<=-)[^-\s]+-

Key Changes

  1. First Segment: (?<=\.\d{3})[^-]+(?=-)

    • Captures text immediately after a dot followed by three digits, up to the next hyphen. This directly targets the desired .0.xx_0 in the problematic string.
  2. Second Segment: (?!.*?\.\d{3}(?=-))(?<=[^-\s]{7})[^-]+(?=-)

    • Adds a negative lookahead (?!.*?\.\d{3}(?=-)) to ensure this segment only runs if there are no dot-three-digit patterns before the final hyphen. This preserves correct matching for strings without three-digit groups.
  3. Third Segment: Unchanged, matches text between two hyphens (e.g., 00000000- in tes-00000000-1111).

Test Results

  • q.0.0.0.1-1111: Matches 1
  • q.0.0.0.1.tt_0-1111: Matches 1.tt_0
  • tes-00000000-1111: Matches 00000000-
  • q.00.00.000.0.xx_0-1111: Matches .0.xx_0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 07:54:52