Regex Help

Post Reply
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Regex Help

Post by TheCaptain »

I'm really struggling with this. I've asked all over the internet, read every document, studied every tutorial... you get the picture!

We have filenames in the following format :

000000a-COMPANY-BRAND-SUBBRAND-Item-Additional Info-V01

I want to be able to plug into the search a term to extract a certain field.
Screen Shot 2018-04-19 at 11.28.01.png
Screen Shot 2018-04-19 at 11.28.01.png (52.4 KiB) Viewed 19716 times
So if the file is to be sorted by Company, regex a match for the second section of the filename. Or if I need to get the Additional Info I can just match for that.

Let me break it down a little further...

000000 ..............: Always 6x digits / Always in this position relative to the left of the string
a .......................: Always a single letter / Always follows the numbers / Will always be the 7th character
- ........................: Always in this position relative to the left of the string / Always a hyphen
COMPANY ...........: Always in this position relative to the left of the string / Always 7x letters
- ........................: Always in this position relative to the left of the string / Always a hyphen
BRAND ................: Always in this position relative to the left of the string / Always 6x letters
- ........................: Always in this position relative to the left of the string / Always a hyphen
SUBBRAND ............: May or may not exist / Could be any combination of any characters
- ........................: May or may not exist
Item ...................: May or may not exist / Could be any combination of any characters
- .......................: May or may not exist
Additional Info ........: May or may not exist / Could be any combination of any characters
-....................... : May or may not exist
V01............: Always exists / Always a capital V followed by two digits / Always in this position relative to the righthand end of the string

I have any number of failed attempts, and to be honest I don't think it would help if I posted them.

Any thoughts? :D
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Regex Help

Post by cstevens »

Would

/([A-Za-z]){7}/

work? That would just match the first string of 7 letters.
Arthur
Member
Posts: 113
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

Re: Regex Help

Post by Arthur »

why can you not use SEGMENT 8-15 ??
If the company is after 7 characters starting at position 8....
That seems the easiest to me. Would it work ??

--- edit ----
actually 9 - 16 :)
As the string for Company starts at pos 9 :)
Last edited by Arthur on Thu Apr 19, 2018 5:11 pm, edited 1 time in total.
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Regex Help

Post by cstevens »

Also, you posted this under the scripting forum. If you have the ability to use scripting then :

Code: Select all

var company = job.getName().split('-')[1];
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Re: Regex Help

Post by TheCaptain »

I posted in the Scripting as it seemed most suited. Unfortunately, we do not have the Scirpting Module for Switch though :|

The reason I can't just use the segment is because the end characters before the 'V01' could be anything from 1 up to 20+ characters. Is there a way to say 'From segment 14 onwards, and stop at -V01'?
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Regex Help

Post by cstevens »

Are you trying to get COMPANY or Additional Info?

Segment starts at the beginning of the name, so as long as the first section is consistent company should always start at 9.
Arthur
Member
Posts: 113
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

Re: Regex Help

Post by Arthur »

(^(\d{6})([a-z])-)(.*)(-V(\d{2}))

this returns the COMPANY-BRAND-SUBBRAND-Item-Additional Info

which is under \4 of the above RegEx.

it may not require the ^ at the beginning. Tested it only in the online regex tool, rather than Switch, which sometimes is a bit fussy as to how you define the RegEx, but I think this should work anyhow. There is no look aheads or anything like this, so shall be fine.
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Re: Regex Help

Post by TheCaptain »

cstevens wrote:Are you trying to get COMPANY or Additional Info?
Trying to get Additional Info
Arthur wrote:(^(\d{6})([a-z])-)(.*)(-V(\d{2}))

this returns the COMPANY-BRAND-SUBBRAND-Item-Additional Info

which is under \4 of the above RegEx.
But how do you return the \4 in Switch. Nothing I try seems to pull out any matches from a string.
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Regex Help

Post by cstevens »

I got this to work in an online RegEx testing tool:

/(?<=-)[\d\w\s]*(?=-V\d{2})/

but javascript/Switch doesn't support lookbehinds. If you leave the lookbehind off:

/[\d\w\s]*(?=-V\d{2})/

it still works, but only because the '-' character is not a digit/alpha/whitespace character. Not sure how reliable that would be.
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Re: Regex Help

Post by TheCaptain »

cstevens wrote:/[\d\w\s]*(?=-V\d{2})/

it still works, but only because the '-' character is not a digit/alpha/whitespace character. Not sure how reliable that would be.
Yes, this almost does what I'm after 8-)

I've adapted it to :

Segment 13-

.*(?=-V\d{2})
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: Regex Help

Post by cstevens »

I didn't know you could combine segment and RegEx like that. Thank you for sharing.
Arthur
Member
Posts: 113
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

Re: Regex Help

Post by Arthur »

TheCaptain wrote: I've adapted it to :

Segment 13-

.*(?=-V\d{2})
I like that, thx for sharing you can combine the two like this :)
NEOSA
Member
Posts: 39
Joined: Thu Mar 10, 2016 6:31 pm

Re: Regex Help

Post by NEOSA »

You can also have a look with this new App :

https://www.enfocus.com/en/appstore/pro ... ngsplitter
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Re: Regex Help

Post by TheCaptain »

Ha! What are the chances.

Nice one for the notification, we'll check it out 8-)
Post Reply