26

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
bryan
  • 385
  • 2
  • 4
  • 7

7 Answers7

38

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

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
9

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];
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
6

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("*"));
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
6
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]
chahuistle
  • 2,627
  • 22
  • 30
  • This is actually exactly what I needed although my question didnt make it seem so...thanks a lot – bryan Feb 15 '11 at 17:54
0
    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(" ");
    }
0

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.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
-2
String str= 21*90'89;
String part= str.split("[*|']");
System.out.println(part[0] +""+part[1]);
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • -1: A) it doesn't compile, B) @Goran Jovic answered essentially the same thing about 11 minutes earlier, C) it prints "2189", which doesn't seem like what the OP is looking for – Rob Hruska Feb 10 '11 at 21:11