regex - 使用 PowerShell 以字符串格式更改 IP 的第三个八位字节
问题描述
认为我找到了最糟糕的方法:
$ip = "192.168.13.1"
$a,$b,$c,$d = $ip.Split(".")
[int]$c = $c
$c = $c+1
[string]$c = $c
$newIP = $a+"."+$b+"."+$c+"."+$d
$newIP
但是最好的方法是什么?完成后必须是字符串。无需担心验证其合法 IP。
解决方案
使用您的示例来了解如何修改第三个八位字节,我会以几乎相同的方式进行操作,但我会将一些步骤压缩在一起:
$IP = "192.168.13.1"
$octets = $IP.Split(".") # or $octets = $IP -split "\."
$octets[2] = [string]([int]$octets[2] + 1) # or other manipulation of the third octet
$newIP = $octets -join "."
$newIP
推荐阅读
- angular - 使用 TypeScript 添加加载图标时出现问题
- python - 'NoneType' 对象不是 python 进度条库的迭代器
- android - 来自 jetified-aws-android-sdk 和 jetified-aws-java-sdk 的重复类
- huawei-mobile-services - 集成HMS Core Safety Detect的SysIntegrity API时报错
- api - 如何加密[隐藏] API URL/数据?
- c# - 如何从 Xamarin.Forms 项目中的本地应用程序文件夹中检索文件列表?
- python - 从 ansible 创建 VM 时遇到一些问题
- java - 将 @ManyToMany 集合映射到 id 集合
- mysql - 在数据库中创建表时导致 SQL 语法错误的原因
- bash - 删除名称中带有 ^M 的文件夹