Use regular expressions to filter array instances in PowerShell _powershell

Source: Internet
Author: User

This article describes using the match operator in PowerShell to filter out the desired content from the array with regular expressions.

Let's look at the following example:

Copy Code code as follows:

PS c:\users\hong> (ipconfig)-match ' IPv4 '
IPV4 Address ............:192.168.1.102
IPV4 Address ............:192.168.193.1
IPV4 Address ............:192.168.93.1

We know that Ipconfig is displaying the TCP/IP configuration information for the current computer, one of which is an IP address. If you have more than one network card, or a Web site configured with multiple IP addresses, can be clearly displayed. To show the match operator's ability to filter arrays, let's take this ipconfig for example.

The results of ipconfig execution should be roughly like this:

Copy Code code as follows:

PS c:\users\hong> (ipconfig)

Windows IP Configuration


Wireless LAN Adapter Local Area Connection * 13:

Media state ............: Media is disconnected
Connection-specific DNS suffix ...:

Wireless LAN Adapter Wi-Fi:

Connection-specific DNS suffix ...:
Local Link IPV6 address ... : fe80::a438:a0ec:e477:94c6%12
IPV4 Address ............:192.168.1.102
Subnet Mask ............:255.255.255.0
Default gateway ............. : 192.168.1.1

...... (Other network adapters are omitted here)


We want to see the IP address of the native configuration, so just look at the line that starts with IPV4. Then we can simply use "IPv4" as a matching keyword. So the PowerShell command can be written:

Copy Code code as follows:

(ipconfig)-match ' IPv4 '

Why do you enclose the ipconfig in parentheses here? Because we know that the ipconfig itself has some parameters, in order not to let the system mistakenly think that-match is also the parameter we assign to ipconfig, we add a pair of parentheses to Ipconfig. Parentheses give priority to the implementation, hehe, is to tell the system, Ipconfig is independent of the whole.

In the example, the result of ipconfig execution is an array of strings. Each element of an array is the output of each row, that is, how many rows of output, there is a number of array elements. The match parameter is an array of actions that find matching elements from the elements of the element and then output the matching elements.

About PowerShell use match to filter the array, this article introduces so much, hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.