2010-06-07 32 views
23

如何使用PowerShell解壓縮子字符串?使用PowerShell解析子字符串

我有這個字符串...

"-----start-------Hello World------end-------" 

我要提取...

Hello World 

什麼是做到這一點的最好方法是什麼?

回答

13

Substring方法爲我們提供了一種基於起始位置和長度從原始字符串中提取特定字符串的方法。如果只提供一個參數,則將其作爲起始位置,並輸出字符串的其餘部分。

PS > "test_string".Substring(0,4) 
Test 
PS > "test_string".Substring(4) 
_stringPS > 

link text

但是,這很容易...

$s = 'Hello World is in here Hello World!' 
$p = 'Hello World' 
$s -match $p 

最後,通過目錄只選擇了.txt文件和搜索的 「Hello World」 的發生遞歸:

dir -rec -filter *.txt | Select-String 'Hello World' 
+2

+1:不過正則表達式捕捉最新的開始和結束標記之間會更好例如「-----開始-------(。+?)------結束-------」(未經測試的正則表達式,我不是正則表達式大師) – 2010-06-09 14:49:10

26

-match運算符測試一個正則表達式,將它與魔變$matches,讓您的結果

PS C:\> $x = "----start----Hello World----end----" 
PS C:\> $x -match "----start----(?<content>.*)----end----" 
True 
PS C:\> $matches['content'] 
Hello World 

每當有關正則表達式-Y的東西疑問,看看這個網站:http://www.regular-expressions.info

+4

我沒有線索在PowerShell中做正則表達式是這個直截了當的!非常感謝!!! – xdhmoore 2012-04-21 03:02:48

+0

在PowerShell中的正則表達式是驚人的。 – 2013-10-04 16:41:56

+0

當我嘗試這個我沒有看到「真正」,但全線,因爲我試圖在多線變量(出乒)。因此,做一個單一的ping和保存答覆我然後只是想要搜索此行「回覆從149.155.224.1:字節= 32時間<1ms TTL = 128」並提取IP地址。你有什麼想法? – DevilWAH 2016-06-10 23:36:36

2

大廈馬特的答案,這裏是一個跨新行搜索,很容易修改供自己使用

$String="----start----`nHello World`n----end----" 
$SearchStart="----start----`n" #Will not be included in results 
$SearchEnd="`n----end----" #Will not be included in results 
$String -match "(?s)$SearchStart(?<content>.*)$SearchEnd" 
$result=$matches['content'] 
$result 

-

注意:如果你想對一個文件運行該記住獲取內容再數組不是一個字符串。您可以通過執行以下操作來解決此問題:

$String=[string]::join("`n", (Get-Content $Filename)) 
+0

更易於使用-raw或者只是讀取文件使用。NET方法 – 4c74356b41 2018-02-09 17:40:40

-2

由於字符串不復雜,因此不需要添加RegEx字符串。一個簡單的比賽會做的伎倆

$line = "----start----Hello World----end----" 
$line -match "Hello World" 
$matches[0] 
Hello World 

$result = $matches[0] 
$result 
Hello World 
+6

提取已知字符串是毫無意義的,因爲您已經知道該字符串。 – Swonkie 2015-02-20 23:52:33

1

其他的解決辦法

$template="-----start-------{Value:This is a test 123}------end-------" 
$text="-----start-------Hello World------end-------" 

$text | ConvertFrom-String -TemplateContent $template 
+0

頭腦風暴。 PowerShell有這樣瘋狂的功能 – 2017-10-27 19:25:56

1
PS> $a = "-----start-------Hello World------end-------" 
PS> $a.substring(17, 11) 
     or 
PS> $a.Substring($a.IndexOf('H'), 11) 
+0

$ a.Substring(argument1,argument2) - 這裏argument1 =所需字母表的起始位置和argument2 =你想要輸出的子串的長度。 這裏17是字母'H'的索引,因爲我們想打印到Hello World,我們提供11作爲第二個參數。 – 2017-03-01 05:39:35

3

不知道這是否是有效與否,但在PowerShell中的字符串可以被稱爲使用數組索引的語法,在與Python類似。

這不是完全直觀的,因爲這樣的事實的第一個字母是由index = 0稱爲,但它確實:

  • 允許第二索引號比該串長,不產生錯誤
  • 在反向
  • 提取子
  • 提取子串從所述字符串的末尾

ħ ERE是一些例子:

PS > 'Hello World'[0..2] 

生成結果(包括對清晰度指數值 - 在輸出不產生):

H [0] 
e [1] 
l [2] 

哪些可以通過傳遞-join ''變得更爲有用:

PS > 'Hello World'[0..2] -join '' 
Hel 

使用不同的索引可以獲得一些有趣的效果:

轉發

使用第一個小於第二個的索引值,並且子字符串將按照您預期的方式在向前的方向上提取。這一次的第二指標值是遠遠超過了字符串的長度,但沒有錯誤:如果提供了第二個指標值

PS > 'Hello World'.Substring(3,300) 
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within 
the string. 

向後

PS > 'Hello World'[3..300] -join '' 
lo World 

不像即低於第一個,字符串返回相反:

PS > 'Hello World'[4..0] -join '' 
olleH 

自最終

如果你使用,你可以參照從字符串結束的位置負數。要提取'World',最後5個字母,我們使用:

PS > 'Hello World'[-5..-1] -join '' 
World