转换对话IRC日志与正则表达式?

时间:2013-04-10 21:57:02

标签: regex irc reformat reformatting

所以这里我有来自BNC的IRC日志格式为(其中[AA:BB:CC]不是实际时间,只是加载时间):

[AA:BB:CC] <Person1> [re:al:ts] BLAH BLAH BLAH
[AA:BB:CC] <Person2> [an:ot:he] BLAH BLAH BLAH
[AA:BB:CC] <Person3> [rr:ea:lt] BLAH BLAH BLAH
[AA:BB:CC] <Person4> [im:es:tp] BLAH BLAH BLAH

我想将其转换为:

[re:al:ts] <Person1> BLAH BLAH BLAH
[an:ot:he] <Person2> BLAH BLAH BLAH
[rr:ea:lt] <Person3> BLAH BLAH BLAH
[im:es:tp] <Person4> BLAH BLAH BLAH

技术上可行吗?我看到[AA:BB:CC]可以很容易地删除,但是如何保留实时时间戳并将它们移动到行/ /行而不删除“等等等等”或“s”?说实话,我并不是真正精通正则表达式......

谢谢:) 枫木

3 个答案:

答案 0 :(得分:2)

要解决的一个例子:

perl -pe 's/^\[..:..:..](.*)(\[..:..:..]) (.*)/$2$1$3/' <<EOT
[AA:BB:CC] <Person1> [re:al:ts] BLAH BLAH BLAH
[AA:BB:CC] <Person2> [an:ot:he] BLAH BLAH BLAH
[AA:BB:CC] <Person3> [rr:ea:lt] BLAH BLAH BLAH
[AA:BB:CC] <Person4> [im:es:tp] BLAH BLAH BLAH
EOT

输出:

[re:al:ts] <Person1> BLAH BLAH BLAH
[an:ot:he] <Person2> BLAH BLAH BLAH
[rr:ea:lt] <Person3> BLAH BLAH BLAH
[im:es:tp] <Person4> BLAH BLAH BLAH

如果AA,BB,CC,...是数字,则在perl正则表达式中使用\d\d代替..

答案 1 :(得分:0)

如果您不使用多行匹配,请尝试使用此正则表达式:

/\[.*?]( <.*?> )\[(.*?)]/g

你将替换为:

"[$2]$1"

答案 2 :(得分:0)

让我们先做一些假设。

  1. 表格[hh:mm:ss]的时间戳,10以下的秒/分表示为01等。
  2. 人物字符串不包含“[”
  3. 然后以下正则表达式将起作用:

    ^\[\d{2}:\d{2}:\d{2}\]([^\[]++)(\[\d{2}:\d{2}:\d{2}\])(.*)$
    

    以下是用Java编写的测试用例:

    public static void main(String[] args) {
        final String[] strings = {"[AA:BB:CC] <Person1> [re:al:ts] BLAH BLAH BLAH",
            "[12:12:11] <Person2> [14:10:25] BLAH BLAH BLAH",
            "[12:12:11] <Person3> [14:10:25] BLAH BLAH BLAH",
            "[12:12:11] <Person4> [14:10:25] BLAH BLAH BLAH"};
        final Pattern pattern = Pattern.compile("^\\[\\d{2}:\\d{2}:\\d{2}\\]([^\\[]++)(\\[\\d{2}:\\d{2}:\\d{2}\\])(.*)$");
        for(final String string : strings) {
            final Matcher matcher = pattern.matcher(string);
            if(matcher.matches()) {
                System.out.println(matcher.group(2) + matcher.group(1) + matcher.group(3));
            }
        }
    }
    

    输出:

    [14:10:25] <Person2>  BLAH BLAH BLAH
    [14:10:25] <Person3>  BLAH BLAH BLAH
    [14:10:25] <Person4>  BLAH BLAH BLAH