java - 创建一个正则表达式来解析文件 - Java

标签 java regex

我有一个我想解析为以下格式的日志文件:

225:org.powertac.common.Competition::0::new::game-0
287:org.powertac.common.Competition::0::withSimulationBaseTime::1255132800000
288:org.powertac.common.Competition::0::withTimezoneOffset::-6
288:org.powertac.common.Competition::0::withLatitude::45
289:org.powertac.common.Competition::0::withBootstrapTimeslotCount::336
289:org.powertac.common.Competition::0::withBootstrapDiscardedTimeslots::24
290:org.powertac.common.Competition::0::withMinimumTimeslotCount::1400
290:org.powertac.common.Competition::0::withExpectedTimeslotCount::1440
291:org.powertac.common.Competition::0::withTimeslotLength::60
291:org.powertac.common.Competition::0::withSimulationRate::720
292:org.powertac.common.Competition::0::withTimeslotsOpen::24
292:org.powertac.common.Competition::0::withDeactivateTimeslotsAhead::1
300:org.powertac.du.DefaultBrokerService$LocalBroker::1::new::default broker
300:org.powertac.du.DefaultBrokerService$LocalBroker::1::setLocal::true
2074:org.powertac.common.RandomSeed::2::init::CompetitionControlService::0::game-setup::5354386935242895562
2157:org.powertac.common.TimeService::null::setCurrentTime::2009-10-10T00:00:00.000Z
2197:org.powertac.common.RandomSeed::3::init::AccountingService::0::interest::-8975848432442556652
2206:org.powertac.common.RandomSeed::4::init::TariffMarket::0::fees::-6239716112490883981
2213:org.powertac.common.msg.BrokerAccept::null::new::1
2214:org.powertac.common.msg.BrokerAccept::null::new::1::null
2216:org.powertac.common.RandomSeed::5::init::org.powertac.du.DefaultBrokerService::0::pricing::8741252857248937781
2226:org.powertac.common.TariffSpecification::6::new::1::CONSUMPTION
2231:org.powertac.common.Rate::7::new
2231:org.powertac.common.Rate::7::withValue::-0.5
2232:org.powertac.common.Rate::7::setTariffId::6

模式如下: 对于新对象:

<id>:<classname>::<order_of_execution>::<new>::<args>

对于方法调用:

 <id>:<classname>::<order_of_execution>::<method_name>::<args>

对于内部类:

 <id>:<classname$innerclass>::<order_of_execution>::<method_name or new>::<args>

对于 init 调用:

 <id>:<classname>::<order_of_execution>::<init>::<args>

我想要一个处理所有案例的正则表达式,并且我将能够检索案例中显示的每个值。 如果我想创建一个新对象,那么我会使用 Java 中的 Reflection API。 所以,例如:

2231:org.powertac.common.Rate::7::new

将被解析为“2231”、“org.powertac.common.Rate”、“7”、“new”、args = {}。 我怎么想出这样的正则表达式?

最佳答案

使用 Matcher与捕获组:

String s = "225:org.powertac.common.Competition::0::new::game-0";
Pattern p = Pattern.compile("([^:]+):([^:]+)::([\\d]+)::([^:]+)::(.+)");
Matcher m = p.matcher(s);
if (m.find()) {
  String id = m.group(1);
  String className = m.group(2);
  int orderOfExecution = Integer.valueOf(m.group(3));
  String methodNameOrNew = m.group(4);
  String[] arguments = m.group(5).split("::");
}

或者更简单的方法,使用 java.util.Scanner , 分隔符设置为 ::?:

Scanner scanner = new Scanner(s);
scanner.useDelimiter("::?");
int id = scanner.nextInt();
String className = scanner.next();
int orderOfExecution = scanner.nextInt();
String methodNameOrNew = scanner.next();
scanner.useDelimiter("$").skip("::");
String[] arguments = scanner.next().split("::");

关于java - 创建一个正则表达式来解析文件 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12148714/

相关文章:

java - Tomcat 没有选择 index.html 上显示的 404

regex - 使用sed在Mac OS X上删除回车

Java GUI JButton 到 Action 监听器

java - 如何将父类(super class)对象值复制到子类对象值?

java - 需要从 RTMP 流录制视频

regex - 带参数的 Expressjs regExp

javascript - 正则表达式接受 Javascript 中的字母数字和一些特殊字符?

regex - Perl 正则表达式不匹配

python - 使用正则表达式从文本创建 python 字典以区分特定部分

java - 为什么要解释java字节码?