Home javascript android java php c# ubuntu python c++ c ios TAGS

Regular Expressions


Date : 2019-08-27 15:08:39 , By : GA 2015

I need to grab two IP addresses from hosts file. I'm new to regular expressions and using the following code. Its working but it's grabbing all the IP's listed.

When running the below script this is the results:

102.54.94.97 38.25.63.10 127.0.0.1 10.104.134.41 10.104.134.42 10.0.45.90 10.0.45.92 10.0.45.93 10.0.45.94 10.0.45.95 10.0.45.96 10.0.45.97 10.0.45.98

$input_path = ‘c:\temp\Hosts’
$output_file = ‘c:\temp\test.txt’
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’
select-string -Path $input_path -Pattern $regex -AllMatches | % { 
$_.Matches } | % { $_.Value } > $output_file

What I'm expecting is to grab just two of the IP's:

10.104.134.41 & 10.104.134.42

The first octets will always be 10 and the last octets will always be .41 & .42

TAGS : regex
Answers :

Your regular expression currently has no restriction on the first/last octet.

$regex = '\b10\.\d{1,3}\.\d{1,3}\.4[12]\b'

See it in action here

- Technology - Yellow pages
» java
» php
» c#
» ubuntu
» python
» c++
» c
» ios
» jquery
» html
» sql
» r
» mysql
» linux
» access
» iphone
» css
» excel
» vb.net
» .net
» ruby
Privacy Policy - Copyrights Notice - Terms - Contact Us 2019 © tinytut
Licensed under cc by-sa 3.0 with attribution required.