代码之家  ›  专栏  ›  技术社区  ›  VKarthik

用于提取节号和描述的正则表达式

  •  0
  • VKarthik  · 技术社区  · 5 年前

    我有以下数据,我想在两个单独的字符串中提取章节号和描述。

    我使用下面的表达- (\d+(?>\.\d+)*)(.\D*) .然而,在我看到的描述中 '. ' 值,我希望该点位于标识的部分。

    有人能帮我吗?

    这是我的示例字符串-

    1.1.1. ABC
    1.1.2. DEF
    1.1.2.3. XYZ
    

    期待-

    No|Description
    1.1.1.|ABC
    1.1.2.|DEF
    1.1.2.3.|XYZ
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   RavinderSingh13 Nikita Bakshi    5 年前

    根据你方展示的样品,请尝试以下内容。 在线演示如下: Online demo of regex

    (?P<serial>(?:\d+\.)+)\s*(?P<description>\S+)
    

    说明: 增加了对以上内容的详细解释。

    (?P<serial>           ##Starting a named capturing group named serial here.
    (?:\d+\.)+)           ##Starting a non capturing group which is looking for continuous occurrence of digits followed with dot with 1 or more occurrences of this combinations.
    \s*                   ##Looking for 1 or more occurrences of spaces here.
    (?P<description>\S+)  ##Starting a named capturing group named description here which has all values except spaces occurrences.