0

Here is some sample input:

<210>   DW_AT_name        : (indirect string, offset: 0x55): double
 <ae>   DW_AT_name        : (indirect string, offset: 0x24): long int
 <b5>   DW_AT_name        : int

I want to extract the string that represents the actual type. So my output would be:

double
long int
int

Here is the regex I have so far (double escaped because it's in Java):

.*DW_AT_name.*:\\s*([^:&&\\S]*)\\s*

It works for the int, but it doesn't work for the other two. I think the best solution is to basically say 'get everything after the last colon' but I'm not sure how. Note that it must also include the DW_AT_NAME stuff.

dda
  • 6,030
  • 2
  • 25
  • 34
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122

4 Answers4

8

You need no regex for this:

String yourString = "<210>   DW_AT_name        : (indirect string, offset: 0x55): double";
String result;
if (yourString.contains("DW_AT_name")) {
    int lastIndex = yourString.lastIndexOf(":");
    result = yourString.substring(lastIndex + 1).trim();
} else {
    result = "ERROR"; // or handle this however you want
}
System.out.println(result);

Simply find the last : and take everything after that. Then trim it to remove leading and trailing whitespace.

I edited my question, it needs to also check for DW_AT_name. String split wont [sic] do that.

Just use contains, then. (edited answer)

tckmn
  • 57,719
  • 27
  • 114
  • 156
0

This regexp ^.*DW_AT_name.*:\s*(.+)$ does the job as just tested on regexplanet.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
0
String type = str.replaceAll(".*:(?=[^:]+$)", "");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • http://stackoverflow.com/questions/17354662/regex-to-find-string-after-last-colon/17354682#comment25183234_17354682 – tckmn Jun 27 '13 at 23:12
0

Try the next:

public static void main(String[] args) {

    String text = 
    "<210>   DW_AT_name        : (indirect string, offset: 0x55): double\n" + 
    "<ae>   DW_AT_name        : (indirect string, offset: 0x24): long int\n" + 
    "<b5>   DW_AT_name        : int";

    Pattern pattern = Pattern.compile("^.*DW_AT_NAME.*:\\s*([^:]+)$", 
            Pattern.CASE_INSENSITIVE);

    Scanner sc = new Scanner(text);
    while(sc.hasNextLine()) {
        String line = sc.nextLine();
        Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
            System.out.println(matcher.replaceAll("$1"));
        }
    }

}

Output:

double
long int
int
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148