Find text in file using Regex

Hi,
I’m trying to find all instances of an IP Address or 6 digits in a text file using regex without success,
I have tried the below small code, the file 123.txt contain IP Addresses and several instances of 6 digits such as 123456,

$content = Get-Content -Path C:\temp\123.txt
$ipv4 = [regex] “\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b”
$content | select-string -Pattern $ipv4
$6digits = [regex] “[1]{1,6}$”
$content | select-string -Pattern $6digits

What am I missing?

Thanks!


  1. 0-9 ↩︎

Amichai,
Welcome to the forum. :wave:t3:

I did not check your regex pattern for IP addresses but assuming it is valid that should actually be enough:

$patternList = '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b','^[0-9]{1,6}$'
Select-String -Path 'C:\temp\123.txt' -Pattern $patternList -AllMatches

BTW: You know you’re looking for IP addresses separated by word boundaries anywhere on the line but the six digit numbers should be the only thing on their lines, don’t you?

And BTW #2:
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )