How to extract part of a filename into another column?

Discuss and share scripts and script files...
Post Reply
dennydraco
Posts: 7
Joined: 25 Jul 2014 16:20

How to extract part of a filename into another column?

Post by dennydraco »

I have file names like;
6A00520301_cREV_02_t.plx
9301-0343-01_cREV_YN_top.plx

That I want to pull out '02' and 'YN' into another custom column.

I started to use regular expression for this in a script for the colum.

$crev = regexmatches(<cc_name>, "cREV_[A-Z0-9]{1,3}");
return $crev;

Right now I get "cREV_02"

How would I match the 'cREV' part in the name and return only the '02' to the custom column?
Thanks ahead. :ninja:

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to extract part of a filename into another column?

Post by highend »

Code: Select all

$crev = regexreplace("<string>/$variable", "^(.*?crev_)(.*?(?=_))(.*$)", "$2");
One of my scripts helped you out? Please donate via Paypal

dennydraco
Posts: 7
Joined: 25 Jul 2014 16:20

Re: How to extract part of a filename into another column?

Post by dennydraco »

highend wrote:

Code: Select all

$crev = regexreplace("<string>/$variable", "^(.*?crev_)(.*?(?=_))(.*$)", "$2");
Sorry for my newbie question but your code looks like it is a replacement command instead of a match command.
If it is a replacement command what is it replacing?
Thanks

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to extract part of a filename into another column?

Post by highend »

regexmatches will match everthing inside it's pattern so you would need an additional replace / regexreplace to get rid of the "cREV_" part so it's easier and faster to use regexreplace in the first place.

Code: Select all

^(.*?crev_)(.*?(?=_))(.*$)
^ = Beginning of line
() () () = three backreference groups (captured into $1, $2, $3)
(.*?crev_) = capture anything up to (including) "crev_"
(.*?(?=_)) = capture the next part up to the first _ but without including it
(.*$) = capture anything till the end of line

Code: Select all

"$2"
Throw away the first and the third capture group and keep only the second...

Voilà, you get only 02 / NY or whatever will be between "crev_" and the next "_".
One of my scripts helped you out? Please donate via Paypal

dennydraco
Posts: 7
Joined: 25 Jul 2014 16:20

Re: How to extract part of a filename into another column?

Post by dennydraco »

Thanks for the breakdown. I'm just starting to get into regular expressions and starting to see the power they can wield.
Thanks again. :beer:

Post Reply