【问题标题】:Regular Expressions (Regex) Java正则表达式 (Regex) Java
【发布时间】:2017-02-15 05:35:04
【问题描述】:

我是 Java 新手。我目前正在使用 Java 中的正则表达式 (regex)。 这是我的问题:我想删除:“基金经理:CI Investments Inc.”这部分通过我的正则表达式。我怎样才能做到? 我的代码部分如下所示。

CIG2306 /CIG3306 /CIG1306 基金经理:CI Investments Inc. 这是我读到的输出之一。我想为此输出专门添加一个正则表达式。因为我不希望从我的输出中删除部分基金经理:CI Investments Inc。

public class Main { String text; private getFundCode(){ Pattern ptnFundCode = Pattern.compile("Fund code\\w?:\\s*(.*)[\\r\\n]+", Pattern.CASE_INSENSITIVE); Matcher mchFundCode = ptnFundCode.matcher(text); if(mchFundCode.find()){ System.out.println(mchFundCode.group(1)); //tempResult+=mchFundCode.group(1)+"; "; } else{ tempResult+="N/A; "; }

【问题讨论】:

  • 请显示您尝试过的代码。
  • 你的代码示例在哪里
  • 您是否需要使用正则表达式,或者字符串替换也可以?
  • public class Main { String text; private getFundCode(){ Pattern ptnFundCode = Pattern.compile("Fund code\\w?:\\s*(.*)[\\r\\n]+", Pattern.CASE_INSENSITIVE); Matcher mchFundCode = ptnFundCode.matcher(text); if(mchFundCode.find()){ System.out.println(mchFundCode.group(1)); //tempResult+=mchFundCode.group(1)+"; "; } else{ tempResult+="N/A; "; }
  • 编辑您对此代码提出的问题

标签: java regex jdk1.8.0


【解决方案1】:

你可以试试这个:

str.replaceAll("Fund manager: CI Investments Inc\.", "");

【讨论】:

  • 它不会删除我文本中的所有基金经理,对吗?因为我将在输出的另一列中使用它们。
  • @Chrisxx 我没有完全关注你,但它确实会删除所有Fund managers 和我放在replaceAll 方法中的以下文本。
【解决方案2】:

只需使用replace

String before = "CIG2306 /CIG3306 /CIG1306 Fund manager: CI Investments Inc.";
String after = before.replace ("Fund manager: CI Investments Inc.", "");

如果你真的想用regex代替,那么你可以用String.replaceAll但是你的规则是什么?

也许

String after = before.replaceAll ("F.*", "");

【讨论】:

  • 谢谢,我会用的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多