正则表达式在最后一个冒号后找到一个字符串 [英] Regex to find a string after the last colon

查看:1658
本文介绍了正则表达式在最后一个冒号后找到一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些示例输入:

<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

这是我到目前为止的正则表达式(双重逃脱,因为它是用Java编写的):

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

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

适用于 int ,但不起作用对于另外两个。我认为最好的解决方案是基本上说在最后一次结肠后得到所有东西,但我不确定如何。请注意,它还必须包含 DW_AT_NAME 内容。

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.


我编辑了我的问题,它还需要检查DW_AT_name。字符串拆分不会[原文如此]。

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)

这篇关于正则表达式在最后一个冒号后找到一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆