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

请求用于检测字符串中项目编号(含标题与子标题)的正则表达式

Hey there! Let's build the regex you need to detect those list item numbers (including both top-level and subheadings) based on your examples.

First, let's break down the patterns we need to cover:

  • Numeric numbers with or without a trailing dot (like 1. or 1)
  • Asterisk bullet points (*)
  • Multi-level numeric numbers (like 1.1 or 2.1)

Here's a flexible regex that can match all these cases, accounting for possible leading whitespace (since lists are often indented):

^\s*(?:(\d+(?:\.\d+)*\.?|\*)\s+)

Let me break down what each part does:

  • ^\s*: Matches any leading whitespace at the start of a line (handles indented lists)
  • (?:...): A non-capturing group to group our different numbering patterns together
    • \d+(?:\.\d+)*\.?: This covers numeric numbers:
      • \d+: Matches one or more digits (the main number)
      • (?:\.\d+)*: Optional repeated groups of a dot followed by digits (handles multi-level like 1.1, 1.1.2, etc.)
      • \.?: Makes the trailing dot optional (so it matches both 1 and 1.)
    • |: The "or" operator to include the asterisk pattern
    • \*: Matches the asterisk bullet point
  • \s+: Matches one or more whitespace characters after the number/bullet (separating it from the content)

If you want to capture the actual number/bullet itself (instead of just detecting it), you can convert the non-capturing group to a capturing group by removing the ?::

^\s*((\d+(?:\.\d+)*\.?|\*)\s+)

Let's test this against your examples:

  1. For 1. Hello 2. World: It will match 1. and 2.
  2. For 1 Hello 2 World: It will match 1 and 2
  3. For * Hello * World: It will match * and *
  4. For 1 Hello1 1.1 Word1 2 Hello2 2.1 World2: It will match 1 , 1.1 , 2 , and 2.1

One quick note: This regex assumes the list items start at the beginning of a line (or with indentation). If you need to match numbers anywhere in a string (not just line starts), you can remove the ^ anchor.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 13:47:46