Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何编写正则表达式来删除和添加指定位置的字符?_Regex_Powershell - Fatal编程技术网

Regex 如何编写正则表达式来删除和添加指定位置的字符?

Regex 如何编写正则表达式来删除和添加指定位置的字符?,regex,powershell,Regex,Powershell,我需要编写一个正则表达式来将vol0eaec8f32c9a98654_uu00000001.转换为vol-0eaec8f32c9a98654。我在PowerShell脚本中执行此操作 我试着用下面的代码去掉\uu $s = "vol0eaec8f32c9a98654_00000001." $s.Substring(0, $s.IndexOf('_')) 谢谢你你可以试试这个: $s = $s -replace "vol","vol-"

我需要编写一个正则表达式来将
vol0eaec8f32c9a98654_uu00000001.
转换为
vol-0eaec8f32c9a98654
。我在PowerShell脚本中执行此操作

我试着用下面的代码去掉
\uu

$s = "vol0eaec8f32c9a98654_00000001."
$s.Substring(0, $s.IndexOf('_')) 
谢谢你

你可以试试这个:

$s = $s -replace "vol","vol-"

根据经验,最好询问
如何实现x
,而不是
如何使用y实现x
。这是因为天气原因

正则表达式适合您的问题,但不是您的思考方式。它用于错误检查。实际的字符串操作是使用
.Replace()
IndexOf()
Substring()
这样做的

$s = vol-0eaec8f32c9a98654
# If the string starts with vol and contains an underscore,
# pick substring from 0 to underscore and replace vol with vol-
if($s -match '^vol.+_.*') { 
    $t = $s.Substring(0, $s.IndexOf('_')).Replace('vol', 'vol-')
    $t
} else {
    Write-Host "Invalid string: $s"
}
# output
vol-0eaec8f32c9a98654

查看错误检查的原因,考虑一个没有下划线的字符串:

$s = "vol0eaec8f32c9a9865400000001."
$s.Substring(0, $s.IndexOf('_')).Replace('vol', 'vol-')                                                         Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At line:1 char:1
+ $s.Substring(0, $s.IndexOf('_')).Replace('vol', 'vol-')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException
轰!由于
IndexOf
未找到下划线,因此
Substring
方法引发异常

如果没有
vol
呢?让我们看看:

# NB vool instead of vol
$s = "vool0eaec8f32c9a98654_00000001."
$s.Substring(0, $s.IndexOf('_')).Replace('vol', 'vol-')                                                         
vool0eaec8f32c9a98654
嗯,嗯!!现在下划线部分处理得很好,但字符串的开头没有更改。这是因为
Replace
查找一个模式,如果找到一个,就会替换它。如果没有,那没关系

故事的寓意:总是检查错误情况。正则表达式通常非常适合输入验证,在某些情况下,正则表达式也是替换输入验证的优秀工具。不要太执着于你需要使用正则表达式的想法——除非你在上正则表达式课

另外,即使输入验证也不需要正则表达式。使用
IndexOf
效果很好。我通常更喜欢正则表达式,但在某些情况下,一些索引查找实际上比复杂的正则表达式模式更容易阅读

# Look for vol and _. The underscore must be after vol.
# If both patterns exist, the substring doesn't ever get invalid argument
if( ($s.IndexOf('vol') -ge 0) -and ($s.IndexOf('_') -gt $s.IndexOf('vol')) ) {
  $s.Substring(0, $s.IndexOf('_')).Replace('vol', 'vol-')
} else {  Write-Host "Invalid string: $s" }

在我发布此问题后,我的要求发生了变化,字符串有两种格式
vol0eaec8f32c9a98654_uu00000001.
vol0eaec8f32c9a98654
,下面的正则表达式适用于我,用于将它们转换为有效的卷ID:

    if ($volumeId.IndexOf('vol') -ge 0) {           
            $validId = $volumeID -replace "_[^ ]*$" -replace "vol", "vol-" 
    }
    else { Write-Host "Invalid Volume ID: $volumeId" }

您使用的是什么语言?我正在使用PowerShell,我尝试使用
.substring()
来去除最后的字符,我需要帮助解决此问题