Page 1 of 1

How to extract part of a filename into another column?

Posted: 05 Sep 2014 19:38
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:

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

Posted: 05 Sep 2014 19:52
by highend

Code: Select all

$crev = regexreplace("<string>/$variable", "^(.*?crev_)(.*?(?=_))(.*$)", "$2");

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

Posted: 05 Sep 2014 20:07
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

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

Posted: 05 Sep 2014 20:13
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 "_".

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

Posted: 05 Sep 2014 20:18
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: