简体   繁体   中英

How to get string between a character and first occurrence of double quotes?

I am trying to get tvg-logo string( http://awebsite/logos/TestChannel.png ) but my current code only works on case one.For case 2 and 3 inputs the code outputs not only the tvg-logo value but also the rest of string after it!could you guys show me how to get ONLY tvg-logo url in all these case ?Thanks

public class HelloWorld {
  public static void main(String[] args) {

    //case 1:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\"";
    //case 2:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\" group-title=\"Movies\"";
    //case 3:
    String s="-1 tvg-logo=\"http://awebsite/logos/TestChannel.png\" tvg-name=\"Test Channel\" group-title=\"Movies\"";

    String icon = s.substring(s.indexOf("tvg-logo") + "tvg-logo".length()).replace("=", "").replace("\"", "").replace("\n", "");

    System.out.println("Logo:"+icon);
  }
}

output for case 3 input:

Logo:http://awebsite/logos/TestChannel.png tvg-nameTest Channel group-titleMovies

Expected output for all types of input cases:

Logo:http://awebsite/logos/TestChannel.png

You need to add a second parameter to the substring call to limit the length of the subtring, like so:

public class Main {
  public static void main(final String[] args) {

    // case 1:
    // String s = "-1 tvg-name=\"Test Channel\"
    // tvg-logo=\"http://awebsite/logos/TestChannel.png\"";
    // case 2:
    String s =
            "-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\" group-title=\"Movies\"";
    // case 3:
    // String s =
    // "-1 tvg-logo=\"http://awebsite/logos/TestChannel.png\" tvg-name=\"Test
    // Channel\" group-title=\"Movies\"";

    String tvgLogo = "tvg-logo=\"";
    int tvgLogoIndex = s.indexOf(tvgLogo) + tvgLogo.length();
    String icon = s.substring(tvgLogoIndex, s.indexOf('"', tvgLogoIndex)).replace("=", "")
            .replace("\"", "").replace("\n", "");

    System.out.println("Logo:" + icon);
  }
}

The s.indexOf('"', tvLogoIndex) call searches for the double quote that closes off the tvg-logo attribute so that you select only the logo url.

Note that if you're parsing HTML documents there are probably cleaner solutions than using subtrings.

Here's another using regex.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {

    public static void main(String[] args) {

        String s="-1 tvg-logo=\"http://awebsite/logos/TestChannel.png\" tvg-name=\"Test Channel\" group-title=\"Movies\"";

        // Pattern matcher looking for shortest string surrounded with escaped quotes
        // following tvg-logo=\"
        // Two groups, first is string tvg-logo and second is the logo string
        Matcher tvgMatcher = Pattern.compile("(tvg-logo)=\\\"([^\\\"]+)\\\"").matcher(s);

        while (tvgMatcher.find()) {
            String icon = tvgMatcher.group(2);
            System.out.println(tvgMatcher.group(1)+": "+icon);
        }
    }   
}

Outputs

tvg-logo: http://awebsite/logos/TestChannel.png
public class HelloWorld
{
    public static void main (String[] args) throws java.lang.Exception
    {
        //case 1:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\"";
    //case 2:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\" group-title=\"Movies\"";
    //case 3:
    String s="-1 tvg-logo=\"http://awebsite/logos/TestChannel.png\" tvg-name=\"Test Channel\" group-title=\"Movies\"";

    String icon="";
    String[] splited = s.split("\\s+");
    for(int i=0;i<splited.length;i++){
        if(splited[i].contains("tvg-logo")){
            icon = splited[i].substring(10);
        }
    }
    //String icon = s.substring(s.indexOf("tvg-logo") + "tvg-logo".length()).replace("=", "").replace("\"", "").replace("\n", "");

    System.out.println("Logo:"+icon);
    }
}

You could insert the line icon=icon.substring(0,icon.indexOf(" ")); to extract it.

public static void main(String[] args) {

    //case 1:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\"";
    //case 2:
    //String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\" group-title=\"Movies\"";
    //case 3:
    String s="-1 tvg-logo=\"http://awebsite/logos/TestChannel.png\" tvg-name=\"Test Channel\" group-title=\"Movies\"";
    String icon = s.substring(s.indexOf("tvg-logo") + "tvg-logo".length()).replace("=", "").replace("\"", "").replace("\n", "");
    icon=icon.substring(0,icon.indexOf(".png")+4);
    System.out.println("Logo:"+icon);
  }

If I am right, this is what happens in you code:

  • your string builder logic ("s.substring ....") works for case 1 because it fits quite well this case;
  • in case 2, you have also "group-title" and you don't manage this case, thus is still continues to appear on the final string;
  • in case 3, the situation is the same as the case 2 and moreover tvg-logo and tvg-name are in different orders.

At the end, your code makes a sort of overfitting for the case 1. Keep the same order inside each string of each case and manage the case in which there is group-title at the end of the string.

Try this (I did not test it):

boolean groupFound = false;
//case 1:
//String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\"";
//case 2: {
//String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\" group-title=\"Movies\"";
//groupFound = true;}
//case 3: {
String s="-1 tvg-name=\"Test Channel\" tvg-logo=\"http://awebsite/logos/TestChannel.png\"  group-title=\"Movies\"";
groupFound = true; }

String icon = s.substring(s.indexOf("tvg-logo") + "tvg-logo".length()).replace("=", "").replace("\"", "").replace("\n", "");
if(groupFound){
   String group = "group-title";
   int groupIndex = String.indexOf(group);
   String newIcon = icon.substring(0, groupIndex);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM