We help IT Professionals succeed at work.

Java regex - help splitting a string

NorthReptile
NorthReptile asked
on
378 Views
Last Modified: 2012-02-09
I am trying to create a regex to split a directory format

e.g.

3 Inches of Blood - 01. Battlecry Under a Winter Sun (2002) [ogg256]

artist - album number. album (year) [quality]

The format of the directories is fairly strict , as shown above, but sometimes the album number may be missing if there is only one album by the same band.

I would like a regex that can handle the album number if it exists or not.

Thanks!
Comment
Watch Question

Sharon SethSoftware Developer
CERTIFIED EXPERT

Commented:
And what should happen depending on whether the album number exists or not?

Author

Commented:
I would like the string to be split into respective variables

String artist
String albumNumber
String album
String year
String quality

So albumNumber will just be empty.

Thanks.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

Each value will be in a group. You would therefore use the group method of the Matcher object to get the values. The groups should be sequential, starting at 1:

String artist = matcher.group(1);
String albumNumber = matcher.group(2);
String album = matcher.group(3);
String year = matcher.group(4);
String quality = matcher.group(5);

Open in new window

Author

Commented:
Great it seems to be working just fine :)

I will test it some more!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.