python 正则匹配一段字符串后一个冒号,正则表达式在最后一个冒号后找到一个字符串...

Here is some sample input:

<210> DW_AT_name : (indirect string, offset: 0x55): double

DW_AT_name : (indirect string, offset: 0x24): long int

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.

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值