Reputation:
I have a log file that logs connection drops of computers in a LAN. I want to extract name of each computer from every line of the log file and for that I am doing this: (?<=Name:)\w+|(-PC)
The target text:
`[C417] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9900 (locked) LanId: | (11/23 10:54:09 - 11/23 10:54:44) | Average limit (300) exceeded while pinging www.google.com [74.125.224.147] 8x
[C445] ComputerName:FRONTOFFICE UserID:YB Yenae Ball Station 7C LanId: | (11/23 17:02:00) | Client is connected to agent.`
The problem is that some computer names have -PC
in them and in some isn't. The expression I have created matches computer without -PC
in their names but it if a computer has -PC
in the name, it treats that as a separate match and I don't want that. In short, it gives me 3 matches, but I want only 2. That's why I need help here, I am beginner in regex.
Upvotes: 2
Views: 49
Reputation: 627380
You may use
(?<=Name:)\w+(?:-PC)?
Details
(?<=Name:)
- a place immediately preceded with Name:
\w+
- 1+ word chars(?:-PC)?
- an optional non-capturing group that matches 1 or 0 occurrences of -PC
substring.Consider using word boundaries if you need to match PC
as a whole word,
(?<=Name:)\w+(?:-PC\b)?
See the regex demo.
Upvotes: 1