1

I am receiving metainformations in a radio player via ICY.
Here is a short example of how this can look:

die neue welle - Der beste Musikmix aus 4 Jahrzehnten! - WELSHLY ARMS - SANCTUARY - Der Mehr Musik-Arbeitstag mit Benni Rettich  

Another example for the meta information stream would be:

SWR1 Baden Württemberg

or

Welshly Arms - Sanctuary

Now I need to extract the title from there, the problem is that this 'meta-information' string can have any format. What I know:

-I know the complete meta information string as showed in the first code section
-I know the station name, which is delivered by another ICY propertie

The first approach was to check if the string contains the station name (I thought if not, it has to be the title):

private boolean icyInfoContainsTitleInfo() {
    String title = id3Values.get("StreamTitle"); //this is the title string
    String icy = id3Values.get("icy-name");  //this is the station name

    String[] titleSplit = title.split("\\s");
    String[] icySplit = icy.split("\\s");

    for (String a : titleSplit) {
        StringBuilder abuilder = new StringBuilder();
        abuilder.append(a);
        for (String b : icySplit) {
            StringBuilder builder = new StringBuilder();
            builder.append(b);
            if (builder.toString().toLowerCase().contains(abuilder.toString().toLowerCase())) {
                return false;
            }
        }
    }
    return true;
}

But that does not help me if title and station are both present in the title string.
Is there a pattern that matches a string followed by a slash, backslash or a hyphen followed by another string?

Has anyone encountered a similiar problem?

5
  • just this part of your code is quite redundant. you don't need the StringBuilder, just use b: StringBuilder builder = new StringBuilder(); builder.append(b); if (builder.toString().toLowerCase().contains(abuilder.toString().toLowerCase())) { return false; } Commented Aug 31, 2018 at 8:49
  • 2
    as for your problem, I guess regex is your best bet Commented Aug 31, 2018 at 8:50
  • thanks for the hint with the string builder, this approach killed me:D. I also think it well be a regex Commented Aug 31, 2018 at 8:52
  • "Now I need to extract the title from there, the problem is that this 'meta-information' string can have any format" Even if you know the station name, how will you find where is the title based on the station name ? Regex or even your way still required that you have an minimum idea of the format that you could receive. Commented Aug 31, 2018 at 9:08
  • There is no specification, every station can send this by its own preference. I only know the two options: swap between title and station anme (covered actually) and the one i showed in the first code snippet Commented Aug 31, 2018 at 9:09

1 Answer 1

2

Since you don't have a specification and each station can send a different format. I would not try to find a "perfect" pattern but simply create a mapping to store each station's format regex to recover the title.

First, create a map

Map<String, String> stationPatterns = new HashMap<>();

Them, insert some pattern you know

stationPatterns.put("station1", "(.*)");
stationPatterns.put("station2", "station2 - (.*)");
...

Then, you just need to get this pattern (where you ALWAYS find one capture group).

public String getPattern(String station){
    return stationPatterns.getOrDefault(station, "(.*)"); //Use a default value to get everything)
}

With this, you just need to get a pattern to extract the title from a String.

Pattern pattern = Pattern.compile(getPattern(stationSelected));
Matcher matcher = pattern.matcher(title);
if (matcher.find()) {
    System.out.println("Title : " + matcher.group(1));
} else {
    System.err.println("The title doesn't match the format");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, thats really a great idea! Thanks a lot
It will be much more manageable like this @ItFreak. BTW: I only gave you the way to extract the data.
Yea I know, but if can can use a regex for each group of patterns I dont need the hardcore all in one solution
Side note: Instead of using- " System.err.println("The title doesn't match the format");" - One would rather log the title that does not match (to some persistence state) and then review your code over time to add new patterns for the anomalies .
@J_D indeed, I just did an error output to specify that this could be manage here. This is not meant to be copy-paste for use. As a second side note, I would also catch the exception from Pattern.compile if the regex is invalid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.