I need to extract two different numbers preceded by two different strings.
Employee Id--> Employee16
(I need 16) and
Employee links--> Employee links:2
(I need 2).
Source String looks like following:
Employee16, Employee name is QueenRose
Working for 46w0d
Billing is Distributed
65537 assigned tasks, 0 reordered, 0 unassigned
0 discarded, 0 lost received, 5/255 load
received sequence unavailable, 0xC2E7 sent sequence
Employee links: 2 active, 0 inactive (max not set, min not set)
Dt3/5/10:0, since 46w0d, no tasks pending
Dt3/5/10:10, since 21w0d, no tasks rcvd
Employee is currently working in Hardware section.
Employee19, Employee name is Edward11
Working for 48w4d
Billing is Distributed
206801498 assigned tasks, 0 reordered, 0 unassigned
655372 discarded, 0 lost received, 9/255 load
received sequence unavailable, 0x23CA sent sequence
Employee links: 7 active, 0 inactive (max not set, min not set)
Dt3/5/10:0, since 47w2d, tasks pending
Dt3/5/10:10, since 28w6d, no tasks pending
Dt3/5/10:11, since 18w4d, no tasks pending
Dt3/5/10:12, since 18w4d, no tasks pending
Dt3/5/10:13, since 18w4d, no tasks pending
Dt3/5/10:14, since 18w4d, no tasks pending
Dt3/5/10:15, since 7w2d, no tasks pending
Employee is currently working in Hardware sectione.
Employee6 (inactive)
Employee links: 2
Dt3/5/10:0 (inactive)
开发者_运维知识库 Dt3/5/10:10 (inactive)
Employee7 (inactive)
Employee links: 2
Dt3/5/10:0 (inactive)
Dt3/5/10:10 (inactive)
Tried with the following:
Employee(\d+)[^\n\r]*[^M]*Employee links:\s+(\d+)
Expecting output to be like:
16 2
19 7
6 2
7 2
But is not listing all the Ids and links. Can anybody help me getting this?
It's easiest to extract from the two different locations as two separate matching steps. It's also by far easiest if you split the whole text up into paragraphs first.
Employee Id--> Employee16
(I need 16)
I'd extract a single one like this:
regexp -line {^Employee(\d+),} $paragraph -> employeeNumber
(You want line matching mode for this task, rather than the default "whole string" matching mode.)
Employee links--> Employee links:2
(I need 2)
For this one, again already assuming that we're only looking at the overall record for a single employee:
regexp -line {^\s+Employee links:\s*(\d+)(.*)$} $paragraph -> links rest
In this case, I've extracted not just the $links
but also the $rest
of the line, since it seems that you might need to be able to think about whether that matters. Of course, it might be that the following is even more useful:
regexp -line {^\s+Employee links:\s*(\d+)(?:\s+active,\s+(\d+)\s+inactive)?} \
$paragraph -> activeLinks inactiveLinks
In this case, the $inactiveLinks
will have an empty string if only the first number was present (which seems to happen when the employee is inactive; you'll need to do a trivial bit of logic to tidy up in that case).
Finally, when using regexp
, don't forget to check the result to see if it matched!
Hope this helps.
I was going to provide a complete answer, but then I read Donal much more helpful tutorial and felt I just couldn't. I will show how to split the text up into paragraphs though:
foreach paragraph [regexp -all -inline {.*?\n{2,}} $text] {
do something with $paragraph
}
In your attempt, I see [^\n\r]*
-- are you sure you have carriage returns in your text as well as newlines?
精彩评论