4

次の形式で解析したいログファイルがあります。

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>

すべてのケースを処理する正規表現が必要で、ケースに示されているように各値を取得できます。新しいオブジェクトを作成する場合は、でReflectionAPIを使用しますJava。したがって、たとえば:

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

「2231」、「org.powertac.common.Rate」、「7」、「new」、args={}に解析されます。どうすればそのような正規表現を思いつくことができますか?

4

3 に答える 3

2

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("::");
于 2012-08-27T20:16:00.577 に答える
1

これらすべてを単一の正規表現に押し込もうとしないでください。パターンごとに1つの正規表現を作成し、一致するパターンが見つかるまで、行ごとにそれを各正規表現に一致させます。次に、それに応じて解析できます。

擬似コード:

for line in file:
    if re.match(patNew, line):
        parseNew(line)
    elif re.match(patMethod, line):
        parseMethod(line)
    ...

一致する正規表現<id>:<classname>::<order_of_execution>::<new>::<args>は次のようになります。

([0-9]+):(.*?)::([0-9]+)::(new)(?:::(.*))?
于 2012-08-27T20:03:59.350 に答える
-1

値はコロンで区切られ、コロン自体を含めることはできないため、エスケープや引用符を付ける必要はありません。必要なのは単純なものだけです。

(.*):(.*)::(.*)::(.*)::(.*)

引数がオプションであると想定される場合は、

(.*):(.*)::(.*)::([^:]*)(?:::(.*))?

値はグループ1〜5にあります。たとえば、ログエントリがコンストラクター呼び出しであるかどうかを確認するには、グループ4が「new」に等しいかどうかを確認します。

于 2012-08-27T20:10:01.283 に答える