DDS
DDS

Reputation: 2479

Regex with capture groups

I' trying to match some patterns with a regex and also being able to capture sub-patterns (full pattern must be valid, sub pattern should be extracted.

Some of my patterns are:

20170408143311101_rdds.22;[email protected]SET_PARAMETER.xml

20170408143311101_rdds.22;[email protected].xml

20170408143311101_rdds.22;[email protected]SET_PARAMETER

20170408143311101_rdds.22;[email protected]

20190502085933954_tel.111;[email protected]TEARDOWN.xml

20190502085933954_tel.111;[email protected]TEARDOWN

20190502085933954_tel.111;[email protected]

20190502085933954_tel.111;[email protected].xml

20190502085928958_tel.0222;[email protected]SET_VARIABLE.xml

20190502085928958_tel.0222;[email protected]SET_VARIABLE

20190502085928958_tel.0222;[email protected]

20190502085928958_tel.0222;[email protected].xml

The subpattern are highlighted as:
date: bold
type: plain
attribute: bold italic
ext: italic

NOTE: the "_", "-" and "." characters used as sub-separators should not be taken into the capture groups.

EX: sub-pattern of

20170408143311101_rdds.22;[email protected]_PARAMETER.xml  

are:

date = 20170408143311101
type = rdds.22;[email protected]
attribute = SET_PARAMETER
ext = xml

Now I tried with

^(?<date>\d{17})_(?<type>.*)-?(?<attribute>\w*)?\.?(?<ext>\w{3})?$
^(?<date>\d{17})_(?<type>.*)(-(?<attribute>\w*))?(\.(?<ext>\w{3}))?$

but collpses type attribute and extension inside the "type" capture group

when

^(?<date>\d{17})_(?<type>.*)-(?<attribute>\w*)\.(?<ext>\w{3})$

does not take into account options

How could I improve the expression to take each pattern (if present) in its capture group

I'm really stuck!

Upvotes: 2

Views: 56

Answers (1)

jspit
jspit

Reputation: 7683

Try that. I have not checked all the data.

^(?<date>\d{17})_(?<type>.+?)(?:-(?<attribute>\w*))?(?:\.(?<ext>\w{3}))?$

Upvotes: 2

Related Questions