I'm having trouble with regex, because I can only match some of my goals.
我正在使用正则表达式,因为我只能匹配我的一些目标。
I have a log file and I must match some of the items and write another txt file. I wrote a Java code for a short example of my code but when I put the whole file, everything gets messed up.
我有一个日志文件,我必须匹配一些项目,并写入另一个文本文件。我为我的代码的一个简短例子编写了一个Java代码,但是当我放入整个文件时,一切都搞砸了。
*052511 074217 0065 02242806000 UNKNOWN U G
*052511 074217 0065 4874 02242806000 UNKNOWN U A
*052511 074218 0065 4874 02242806000 UNKNOWN U R
-------- 05/25/11 07:42:17 LINE = 0065 STN = 4874
CALLING NUMBER 02242806000
NAME UNKNOWN
UNKNOWN
BC = SPEECH
00:00:00 INCOMING CALL RINGING 0:02
00:00:11 CALL RELEASED
I have to find these results from the file:
我必须从文件中找到这些结果:
incomming,05/25/11,07:42:17,0065,4874,02242806000,00:00:09,2
In this expression 00:00:09
means [00:00:11-00:00:00]-0:02
在这个表达式中,00:00:09表示[00:00:11-00:00:00] -0:02
For every incoming and outgoing calls, I must make the conversation above.
对于每个来电和去电,我必须进行上述对话。
- Here is my code
- Here is the log file
这是我的代码
这是日志文件
1 个解决方案
#1
0
You could use a regex like:
您可以使用正则表达式:
(?xm:
^-------- \s+ (\S+) \s+ (\S+) \s+ LINE\s*=\s*(\d+) \s+ STN\s*=\s*(\d+)
\s+ CALLING\ NUMBER \s+ (\d+) \s*
(?:^(?:[ \t]+.*)?[\n\r]+)* # eat unwanted part
^(\d\d:\d\d:\d\d) \s+ INCOMING\ CALL \s+ RINGING\ ([\d:]+) \s*
(?:^\d.*[\r\n]+)* # possible stuff
^(\d\d:\d\d:\d\d) \s+ CALL\ RELEASED
)
Use the values of the capturing groups to get your results. You may need to remove the /x
related things like comments and spaces.
使用捕获组的值来获取结果。您可能需要删除/ x相关的内容,如注释和空格。
Perl example at http://ideone.com/qTBFe
在http://ideone.com/qTBFe上的Perl示例
#1
0
You could use a regex like:
您可以使用正则表达式:
(?xm:
^-------- \s+ (\S+) \s+ (\S+) \s+ LINE\s*=\s*(\d+) \s+ STN\s*=\s*(\d+)
\s+ CALLING\ NUMBER \s+ (\d+) \s*
(?:^(?:[ \t]+.*)?[\n\r]+)* # eat unwanted part
^(\d\d:\d\d:\d\d) \s+ INCOMING\ CALL \s+ RINGING\ ([\d:]+) \s*
(?:^\d.*[\r\n]+)* # possible stuff
^(\d\d:\d\d:\d\d) \s+ CALL\ RELEASED
)
Use the values of the capturing groups to get your results. You may need to remove the /x
related things like comments and spaces.
使用捕获组的值来获取结果。您可能需要删除/ x相关的内容,如注释和空格。
Perl example at http://ideone.com/qTBFe
在http://ideone.com/qTBFe上的Perl示例