简体   繁体   中英

Java- Extract part of a string between two special characters

I have been trying to figure out how to extract a portion of a string between two special characters ' and " I've been looking into regex, but frankly I cannot understand it.
Example in Java code:

String str="21*90'89\""; 

I would like to pull out 89

In general I would just like to know how to extract part of a string between two specific characters please.

Also it would be nice to know how to extract part of the string from the beginning to a specific character like to get 21.

Try this regular expression:

'(.*?)"

As a Java string literal you will have to write it as follows:

"'(.*?)\""

Here is a more complete example demonstrating how to use this regular expression with a Matcher :

Pattern pattern = Pattern.compile("'(.*?)\"");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

See it working online: ideone

If you'll always have a string like that (with 3 parts) then this is enough:

 String str= "21*90'89\"";
 String between = str.split("\"|'")[1];

Another option, if you can assure that your strings will always be in the format you provide, you can use a quick-and-dirty substring/indexOf solution:

str.substring(str.indexOf("'") + 1, str.indexOf("\""));

And to get the second piece of data you asked for:

str.substring(0, str.indexOf("*"));
public static void main(final String[] args) {
    final String str = "21*90'89\"";
    final Pattern pattern = Pattern.compile("[\\*'\"]");
    final String[] result = pattern.split(str);
    System.out.println(Arrays.toString(result));
}

Is what you are looking for... The program described above produces:

[21, 90, 89]
    String str="abc#defg@lmn!tp?pqr*tsd";               
    String special="!?@#$%^&*()/<>{}[]:;'`~";           
    ArrayList<Integer> al=new ArrayList<Integer>();         
    for(int i=0;i<str.length();i++)
    {
        for(int j=0;j<special.length();j++)
            if(str.charAt(i)==special.charAt(j))        
                al.add(i);
    }
    for(int i=0;i<al.size()-1;i++)
    {
        int start=al.get(i);
        int end=al.get(i+1);
        for(int j=start+1;j<end;j++)
            System.out.print(str.charAt(j));
        System.out.print(" ");
    }

I'm missing the simplest possible solution here:

str.replaceFirst(".*'(.*)\".*", "$1");

This solution is by far the shortest, however it has some drawbacks:

  • In case the string looks different, you get the whole string back without warning.
  • It's not very efficient, as the used regex gets compiled for each use.

I wouldn't use it except as a quick hack or if I could be really sure about the input format.

String str= 21*90'89;
String part= str.split("[*|']");
System.out.println(part[0] +""+part[1]);

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