1

I'm a complete noob when it come to regex, Can someone please help with this?

What I need is the domain, then I will need to replace all the (??) with a .

What I need Extracted: (11)ops-findb01(13)domain(3)com(0)

End result should look like: .ops-findb01.domain.com.

8/2/2012 3:37:59 PM 0908 PACKET  0000000002CC4F60 UDP Rcv 10.10.10.10  563a   Q [0001   D   NOERROR] A     (11)ops-findb01(13)domain(3)com(0)
8/2/2012 3:37:59 PM 0908 PACKET  0000000002C32810 UDP Rcv 10.10.10.10  6f24   Q [0001   D   NOERROR] A     (11)ops-findb01(13)domain(3)com(0)
8/2/2012 3:38:00 PM 0908 PACKET  00000000029EDC00 UDP Rcv 10.10.10.10  459a   Q [0001   D   NOERROR] A     (3)www(6)google(3)com(0)
8/2/2012 3:38:00 PM 0908 PACKET  0000000002CC4F60 UDP Rcv 10.10.10.10  d47e   Q [0001   D   NOERROR] PTR   (2)dr(7)_dns-sd(4)_udp(1)0(2)40(1)5(2)10(7)in-addr(4)arpa(0)
8/2/2012 3:38:00 PM 0908 PACKET  0000000002C32810 UDP Rcv 10.10.10.10  0b3c   Q [0001   D   NOERROR] PTR   (2)db(7)_dns-sd(4)_udp(1)0(1)0(1)5(2)10(7)in-addr(4)arpa(0)
8/2/2012 3:38:00 PM 0908 PACKET  00000000029EDC00 UDP Rcv 10.10.10.10  8890   Q [0001   D   NOERROR] A     (2)dl(6)javafx(3)com(0)
8/2/2012 3:38:00 PM 0908 PACKET  0000000002CC4F60 UDP Rcv 10.10.10.10  60e7   Q [0001   D   NOERROR] A     (7)trading(9)scottrade(3)com(0)

The log file format will always be what you see above

2 Answers 2

1

Assuming one line at a time:

$betterline = ($line -match "\(\d+\)(?:\w+\(\d+\))+$") -replace "\(\d+\)", "."

This first matches the line to grab the end part, then replaces the numbers and parentheses with a dot.

For the future, I strongly recommend you learn regex - it's simple yet invaluable. A great reference/tutorial for regex is regular-expressions.info.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!! Found 1 issue, should have been [\w-] because it needed to search for hyphen's. See i'm learning :o)
1

Here's another option that doesn't make extensive use of regular expressions:

Get-Content file.txt | Foreach-Object {

    #split the line by space
    $line = $_.split() 

    # get the last element and replace (one or more digits) with a dot
    $line[-1] = $line[-1] -replace '\(\d+\)','.' 

    #join the line back using a space
    $line -join ' ' # join the line back using a space

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.