Regular expression to extract Section Number and Description

Viewed 71

Am having following data and I would like to extract section number and description in two separate strings.

I am using the following expression - (\d+(?>\.\d+)*)(.\D*). However, in the description I am seeing '. 'value and I want the dot to be in the section identified.

Can someone help me with this?

Here are my sample strings -

1.1.1. ABC
1.1.2. DEF
1.1.2.3. XYZ

Expecting -

No|Description
1.1.1.|ABC
1.1.2.|DEF
1.1.2.3.|XYZ
1 Answers

Based on your shown samples, could you please try following. Online demo of this is: Online demo of regex

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

Explanation: Adding detailed explanation for above.

(?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.
Related