filippo
filippo

Reputation: 5783

Alternative capture groups in regex?

So, I have these two file names:

SomeFile_08_20110225153831.txt.gz
ThatOtherFile_15_20110411125902.txt_11.gz

I wanted to do a regex that would always return me date up to the day in the file name for either of the files. Thought this would sort it:

^SomeFile_.*?_([0-9]{8}).*|ThatOtherFile_.*?_([0-9]{8}).*$

(see it in RegExr)

Currently it only capture whatever comes first in the expression...

Eventually, I'll use it in a Oracle replace of the sort:

REGEXP_REPLACE(
    file_name,
    '^SomeFile_.*?_([0-9]{8}).*|ThatOtherFile_.*?_([0-9]{8}).*$',
    '\1')

I have it in a case with two separated expressions right now and it works, but you know, I wish it was prettier :)

Thanks!

f.

Upvotes: 3

Views: 9693

Answers (2)

nobody
nobody

Reputation: 10635

^(SomeFile|ThatOtherFile)_.*?_([0-9]{8}).*$

and access the date with \2, or if oracle supports ?: or equivalent use it to make the first group non-capturing and access the date with \1.

Upvotes: 8

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Please try this regex and verify if you are still getting the same issue:

^(?:SomeFile|ThatOtherFile)_.*?_([0-9]{8}).*$

Upvotes: 2

Related Questions