【问题标题】:examples on how to parse string in java关于如何在java中解析字符串的示例
【发布时间】:2016-02-04 00:07:06
【问题描述】:

我有以下字符串,我需要从中解析/提取“20000”。

"where f_id = '20000' and (flag is true or flag is null)"

对最好的方法有什么建议吗?

这里有更多代码可以帮助理解:

List<ReportDto> reportDtoList = new ArrayList<ReportDto>();
for (Report report : reportList) {
List<ReportDetailsDto> ReportDetailsDtoList = new ArrayList<ReportDetailsDto>();

ReportDto reportDto = new ReportDto();
reportDto.setReportId(report.getReportId());
reportDto.setReportName(report.getName());

Pattern p = Pattern.compile("=\\s'[0-9]+'");
String whereClause = report.getWhereClause();
Matcher m = p.matcher(whereClause);

不知道之后该怎么办?

【问题讨论】:

  • 您的问题不完整。请添加更多细节。
  • split 就可以了
  • 我基本上需要获取该数值并将其设置到我的 dto 中
  • str.replaceAll("\\D", "")

标签: java string parsing substring


【解决方案1】:

您可以使用此正则表达式从您的 String 中提取单个非负整数

Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(text);

if (m.find()) {
    System.out.println(m.group());
}

或者如果你想保留单引号:

Pattern p = Pattern.compile("['0-9]+");

这将提取包含'=' 和其后的单个空格的模式。它将打印一个String,其中包含没有'=' 或空格的数字。所以如果这匹配你就知道'='后面有一个数字

Pattern p = Pattern.compile("=\\s'[0-9]+");
Matcher m = p.matcher(text);

if (m.find()) {
    System.out.println(m.group().substring(3));
}

编辑 根据您添加的代码,这就是它的样子

List<ReportDto> reportDtoList = new ArrayList<ReportDto>();
Pattern p = Pattern.compile("=\\s'[0-9]+");
for (Report report : reportList) {
    List<ReportDetailsDto> ReportDetailsDtoList = new ArrayList<ReportDetailsDto>();

    ReportDto reportDto = new ReportDto();
    reportDto.setReportId(report.getReportId());
    reportDto.setReportName(report.getName());

    String whereClause = report.getWhereClause();
    Matcher m = p.matcher(whereClause);
    if (m.find()) {
        String foundThis = m.group().substring(3);
        // do something with foundThis
    } else {
        // didn't find a number or =
    }
}

【讨论】:

  • 我应该将它包装在 if 语句中,然后检查字符串是否包含“=”,然后我可以从字符串中解析出数字吗? if (a.getWhereClause().contains("="){ aDto.setFId(a.getWhereClause()..... } 这就是我有点困惑的地方
  • 谢谢 Manos - 我可以在此处添加一些内容来排除字符串中返回的单引号吗?当我使用您的示例时,我的 lastMatch 是:'20000 '10000 etc...
  • 对不起,我还是有点困惑。我将如何摆脱单引号?
  • 您好,您对我添加的代码编辑了解更多吗?
  • 谢谢,刚刚看到你添加的内容!
【解决方案2】:

对于浮点数和负数

Pattern p = Pattern.compile("(-?\\d+)(\\.\\d+)?"); 
        String s = "where f_id = '20000' 3.2 and (flag is true or flag is null)";
        Matcher m = p.matcher(s); 
        List<String> extracted = new ArrayList<String>();
        while (m.find()) { 
           extracted.add(m.group());
        }

        for (String g : extracted)

            System.out.println(g);

打印出来

20000
3.2

【讨论】:

  • 另一个问题 - 一旦我得到字符串值,我将如何将其设置为变量,然后能够将字符串值转换为整数,以便我可以将其设置到我的 DTO 中?跨度>
  • 你可以只使用 Integer.parseInt("2000")
  • 所以根据我的代码,提取的列表已经包含了你所有的号码。只需一个一个地解析它们
【解决方案3】:

试试这个:

Pattern p = Pattern.compile("-?\\d+"); 
String s = "your string here";
Matcher m = p.matcher(s); 
List<String> extracted = new ArrayList<String>();
while (m.find()) { 
   extracted.add(m.group());
}

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2014-06-29
    • 2010-12-02
    • 2021-08-24
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多