Regular Expression Help (Again)
Regular Expression Help (Again)
Author
Discussion

dibbers006

Original Poster:

14,640 posts

249 months

Tuesday 17th April 2018
quotequote all
The initial message was deleted from this topic on 15 December 2023 at 22:32

threadlock

3,211 posts

285 months

Tuesday 17th April 2018
quotequote all
What language? In PHP I'd use explode() to do this. Easy peasy smile

zippy3x

1,384 posts

298 months

Tuesday 17th April 2018
quotequote all
think Regex is the wrong thing to use personally. Most languages have some variant of split/explode to do this sort of thing.

Either way (regex or code), your main problem will be the fact that it's hyphen delimited, what happens when a company name has a hyphen in it?

Can you change the delimiting hyphen to something more uncommon in general usage? The pipe "|" symbol is a common delimiter.

TonyRPH

13,558 posts

199 months

Tuesday 17th April 2018
quotequote all
dibbers006 said:
I've got a bundle of files...
<snip>
Do you want to rename the files?

Or are you just wanting to create a categorised list from the file names?

What is the "123456" does that matter?

What operating system is it?



tribbles

4,179 posts

253 months

Tuesday 17th April 2018
quotequote all
If the first sequence is digits, then to get each section between hyphens, and allowing hyphens in the company name, then I would start with:

^(\d+)-(.*)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)$

That was fun to type on a phone!

If I was on my computer at home, I would've tried it out.

Although, as has been mentioned, a string split on "-" would be easier...

TonyRPH

13,558 posts

199 months

Tuesday 17th April 2018
quotequote all
Are you able to work on the files after the software has processed them?

Being in Mac / OSX would make it quite trivial.

But if you have to do it within the software then I've no idea!

TonyRPH

13,558 posts

199 months

Tuesday 17th April 2018
quotequote all
If the files are stored in a single folder somewhere, and you can access them - you could do something like this:

Filename: 123456-COMPANY-BRAND-ITEM-VERSION.txt

ls 123456* |awk -F "-" '{print $1 "\n" $2 "\n" $3 "\n" $4}'

123456
COMPANY
BRAND
ITEM

That separates the data held within the file name which I assume is what you are wanting?



tribbles

4,179 posts

253 months

Tuesday 17th April 2018
quotequote all
dibbers006 said:
Tribbles!

That looks awesome but it's not finding any matches?

LINKY

... I'm going to presume it's my fault... whistle
I did say that I didn't have time to try it smile

Remove the last "-([^\-]+)" from it - I couldn't see your example when I was typing it in, and I accidentally matched too many things.



Each item is a group in its own right.

And if the company name has hyphens in it:



I did spend around 4 years basically writing regular expressions!

maffski

1,905 posts

190 months

Tuesday 17th April 2018
quotequote all
tribbles said:
...And if the company name has hyphens in it
This isn't a valid assumption to make - what if the brand has a hyphen?

tribbles

4,179 posts

253 months

Tuesday 17th April 2018
quotequote all
My one wouldn't work in that case. And I can't think of anything that would cope in a completely generic case - such as one or more hyphens in one or more fields.

Apart from a dictionary of brands which could be placed in an 'or' clause....

TonyRPH

13,558 posts

199 months

Wednesday 18th April 2018
quotequote all
dibbers006 said:
I'm happy to be branded ignorant here, but where would one enter the highlighted line? Is that a Terminal command?

TonyRPH said:
If the files are stored in a single folder somewhere, and you can access them - you could do something like this:

ls 123456* |awk -F "-" '{print $1 "\n" $2 "\n" $3 "\n" $4}'
<snip>
Yes, it's a terminal command.

How it works: 'ls' is the command to list files. The command "awk" breaks the line into columns, based on the delimiter of your choosing (in this case a '-')

The output of ls is 'piped' to awk using the '|' (pipe or more character)

'print' just 'tells' awk to print each column separated by the '-', $1 is the first column, $2 the second and so on.

The "\n" is a line feed to put each matched column on a new line.

So if you omit $1 it looks like this:

ls 123456* |awk -F "-" '{print $2 "\n" $3 "\n" $4}'
COMPANY
BRAND
ITEM


TonyRPH

13,558 posts

199 months

Wednesday 18th April 2018
quotequote all
I was wondering if you were able to access the area where the files are stored.

Assuming the data is stored as files on the local disk (which was the implication in your OP).

So you could just extract the required data that way.